2017-04-23 70 views
1

正在为类进行项目工作,我想要一个javascript事件,该页面加载时弹出,当用户导航时弹出。我试图通过onloadonunload事件直接嵌入到我的html中来处理它,但它似乎并没有在Chrome上工作,而在Internet Explorer上,我似乎只能得到卸载消息。代码如下:页面加载时的Javascript提醒以及页面退出时的提醒

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8"> 
    <title>teest</title> 
    <!-- [if lt IE 9]> 
    <script src = "http://html5shim.googlecode.com/svn/trunk/html5.js"></script> 
    <![endif] --> 
    <link rel="stylesheet" href="css/contact.css" /> 
    <meta name="viewport" content="width=device-width,initial-scale=1.0"> <!-- viewport tag configuration --> 
    <link rel="shortcut icon" href="img/favicon.ico" /> 
    </head> 
    <body onload = "alert('Even if you aren't interested, please give our design some feedback as we would like to stay current.')" onunload = "alert('Thank you for your time and consideration!')"> 
     <header class = "head"> 
      <h1> 
      <img src="img/coa.png" display="inline-block" height="150px" float="left"/> 
      other stuff</h1> 
      <p>stuffsy</p> 
     </header> 

+0

可能的复制或页面刷新?](http://stackoverflow.com/questions/13443503/run-javascript-code-on-window-close-or-page-refresh) – Bassie

+0

[文档的卸载事件](https://开发人员.mozilla.org/en-US/docs/Web/Events/unload)指出“UI inter操作无效(window.open,alert,confirm等)“,所以你不能在onunload中使用alert。 – user2313067

回答

3

你要逃避你的报价在警报。
您当前字符串是

'Even if you aren't interested, please give our design some feedback as we would like to stay current.' 

而且你必须将它更改为

'Even if you aren\'t interested, please give our design some feedback as we would like to stay current.' 

所以它会识别您的字符串以正确的方式

的[运行JavaScript代码窗口关闭
+0

你知道为什么'onunload()'事件在chrome中不起作用吗? – Callat

+1

Chrome在onunload事件中自动阻止警报消息 https://i.stack.imgur.com/ihX2X.png –

+0

啊我明白了,谢谢。我需要为它找到解决方法,因为我的任务需要它。 – Callat