2009-07-17 163 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>