2009-07-17 103 views

回答

12

不可以。您只有onbeforeunload事件,可用作原始javascript(jquery不包含此事件)。

如果你想嘗試一下,試着在這裏發佈一個答案,並且不用按「發佈你的答案」,嘗試關閉瀏覽器窗口。

這是訪問「關閉窗口」事件的最近路線。

+0

不是「否」有點誤導性答案? – 2015-03-16 17:01:07

0

不是。頁面卸載事件是你所有的。即使這並非總是如此。它會提供干擾用戶願望的能力。

1

以下是有關這個好文章從4guysfromrolla

<script language="JavaScript"> 
     window.onbeforeunload = confirmExit; 
     function confirmExit() 
     { 
      return message to display in dialog box; 
     } 
    </script> 
3

onbeforeunload事件捕捉每一個卸載事件,但有一些竅門的方式通過點擊關閉按鈕來處理,如果用戶關閉瀏覽器,這裏是一個示例代碼

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
        "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
    <script src="http://code.jquery.com/jquery-latest.js"></script> 

    <script> 
    $(document).ready(function(){ 

    //handle if your mouse is over the document body, 
    //if not your mouse is outside the document and if you click close button return message or do what you want 

    var out = false; 
    $("body").mouseover(function(){ 
     out=false; 
    }).mouseout(function(){ 
     out=true; 
    }); 


    $(window).bind('beforeunload', function(e){ 
    if(out) 
     {return "Do you really want to leave this page now?"} 
    }); 

    }); 
    </script> 

</head> 
<body> 
    <p> 
    <a href="http://cssbased.com">go outside</a> 
    <br> 
    <a href="test.html">reload page</a> 
    <span> </span> 
    </p> 
</body> 
</html>