离开页面前显示确认提示对话框(兼容IE,firefox) = how to Catch Win...

http://blog.163.com/lgh_2002/blog/static/44017526201061334953256/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<title>教师直通车网</title>
    <script type="text/javascript">
    window.onbeforeunload = function()
    {
       return ('您确定要离开此页面吗?');
    }
    </script>
</head>
<body>
<a href="http://www.jiaoshiok.com">教师直通车网</a>
</body>
</html>

=======================================================================================

Once in a while people ask me how to catch the IE window’s close event. One thing to use is onbeforeunload event of the window. But the problem in using this event is, you never know when this onbeforeunload event is called. This event could be called for numerous reason like navigation to another page with in the application. So you cannot exactly catch the browser close event using onbeforeunload.

The mouse pointer position comes in handy in this cases. We can effectively harness the power of both onbeforeunloadand clientY position to exactly catch the browser close event.

Code: 

window.onbeforeunload = onunload; 
function onunload() 

var closeEvent=false; 
if (event.clientY < 0) 
closeEvent = true;

if ((closeEvent)) 

//Write your code here for close event. 

}

Is there any other efficient way to do this?

Update:

Anonymous: This so very well works with internet explorer 8; i have also tested the tabs it works well. I have given the full code here. However, obviously, there is a requirement: You must allow the execution of javascript.

<HTML> 
<head> 
<script>

    window.onbeforeunload = closeit;

    function closeit() { 
        var closeEvent = false; 
        alert(event.clientY); 
        if (event.clientY < 0) 
            closeEvent = true;

        if ((closeEvent)) { 
            alert("do you want to close the window"); 
            //your actions during closing comes here 
        } 
    }

</script> 
</head> 
<body> 
  <a href="http://blog.163.com/lgh_2002/blog/http://chillicode.wordpress.com">Click here to navigate to 
      Chillicode</a> 
</body> 
</html>

The above code helps us to catch the window close event and there by we can do what ever we wish to do.

In some cases it is enough to just alert the user that he is trying to close the window; how to achieve it? That is even more simple; just return a string value in the closeit function

function closeit()
{
  return "Any string value here forces a dialog box to \n" +
         "appear before closing the window.";
}

转载于:https://my.oschina.net/sunyh/blog/110555

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值