2017-04-23 84 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