2011-03-31 19 views
2

我想事件onbeforeunload類似下面,它工作正常,當我關閉瀏覽器離開現場JavaScript事件 - onbeforeunload - 刷新頁面/內部的超鏈接

<script type="text/javascript" language="javascript"> 
    window.onbeforeunload = LeavingSiteHandler; 
    function LeavingSiteHandler(e) { 
     if (!e) 
      e = window.event; 
     //e.cancelBubble is supported by IE - this will kill the bubbling process. 
     e.cancelBubble = true; 
     e.returnValue = 'You sure you want to leave?'; //This is displayed on the dialog 

     //e.stopPropagation works in Firefox. 
     if (e.stopPropagation) { 
      e.stopPropagation(); 
      e.preventDefault(); 
     } 
    } 
</script> 

然而,當我按F5刷新頁面或點擊在內部鏈接上,此事件將被解僱。是否有可能檢查點擊的超鏈接是否是內部鏈接?如果我按F5,是否可以停止彈出消息? 非常感謝!

+0

除非事情已經改變,該網站說,這基本上是不可能的:http://www.webdeveloper.com/forum/showthread.php?t=202588。 – pimvdb 2011-03-31 14:51:40

回答

2

你可以寫一個onclick處理程序在你的頁面的鏈接,在卸下window.onbeforeunload處理程序。

但你不能頁面刷新和關閉標籤/窗口之間進行區分。

+0

所以,你有一個替代的,而不是onbeforeunload? – khoailang 2011-04-01 03:30:29