如何使用javascript在Firefox中获取浏览器关闭事件? [英] How to get browser close Event in firefox using javascript ?

查看:112
本文介绍了如何使用javascript在Firefox中获取浏览器关闭事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我必须检查浏览器关闭事件的某些条件意味着当我关闭浏览器(当我点击[x]时)那个事件我想要使用javascript。

i我能在IE中做到这一点但在firfox中它无法正常工作

i尝试



  var  altKey =  false ; 
var keyCode = 0 ;

函数closeSession(evt){

evt =(evt)? evt: event ;

clickY = evt.clientY;
altKey = evt.altKey;
keyCode = evt.keyCode;

if (!evt.clientY){
// FireFox中的窗口关闭
// 捕获ALT + F4
keyVals = document.getElementById(' ffKeyTrap');
if (keyVals。 value == ' true115'){
return ' close 1';
}

如果(keyVals。 value == ' '){
// 捕获靠近X的窗口?
// 我们没有密码代码
return ' close 2';
}

} 其他 {
// IE中的窗口关闭
// 捕获ALT + F4
如果(altKey == true && keyCode = = 115 ){
alert(' close 1' );
// 捕获靠近X的窗口
} else if (clickY < 0 ){
alert(' close 2' );
// 只需通过链接离开页面
} else {
// alert('close 3');
return void 0 );
}
}
}

函数whatKey(evt){
evt =(evt)? evt: event ;
keyVals = document.getElementById(' ffKeyTrap');
altKey = evt.altKey;
keyCode = evt.keyCode;
if (altKey&& keyCode == 115 ){
keyVals 。 value = 字符串(altKey)+ 字符串(键代码);
}
}

window.onkeydown = whatKey;
window.onbeforeunload = closeSession;

解决方案

请尝试如下。



 <   html  >  
< body >
< script 类型 = text / javascript >
window.onbeforeunload = check;
功能检查()
{
返回你确定要退出此页吗?;
//或放置当用户关闭web //浏览器时需要调用的任何函数。
}
< / script >
< / body >
< / html >





阅读更多:如何使用javascript检测mozilla firefox窗口关闭



在Firefox,IE和Chrome中使用Javascript关闭事件


Hi all,

I have to check some condition on browser close event means When i close the browser ( when i click on [x] ) that event i want using javascript.
i am able to do this in IE but in firfox itis not working properly
i tried

var altKey  = false;
var keyCode = 0;

function closeSession(evt){

    evt = (evt) ? evt : event;

    clickY  = evt.clientY;
    altKey  = evt.altKey;
    keyCode = evt.keyCode;

    if(!evt.clientY){
        // Window Closing in FireFox
        // capturing ALT + F4
        keyVals = document.getElementById('ffKeyTrap');
        if(keyVals.value == 'true115'){
            return 'close 1';
        }

        if(keyVals.value == ''){
            // capturing a window close by "X" ?
            // we have no keycodes
            return 'close 2';
        }

    } else {
        // Window Closing in IE
        // capturing ALT + F4
        if (altKey == true && keyCode == 115){
            alert('close 1');
        // capturing a window close by "X"
        } else if(clickY < 0){
            alert('close 2');
        // simply leaving the page via a link
        } else {
            //alert('close 3');
            return void(0);
        }
    }
}

function whatKey(evt){
    evt = (evt) ? evt : event;
    keyVals = document.getElementById('ffKeyTrap');
    altKey  = evt.altKey;
    keyCode = evt.keyCode;
    if(altKey && keyCode == 115){
        keyVals.value = String(altKey) + String(keyCode);
    }
}

window.onkeydown      = whatKey;
window.onbeforeunload = closeSession;

解决方案

Please try as below.

<html>
<body>
<script type="text/javascript">
window.onbeforeunload = check;
function check()
{
return "Are you sure you want to exit this page?";
//or put whatever function you need to call when a user closes the web //browser.
}
</script>
</body>
</html>



Read for more : how to detect mozilla firefox window close using javascript

Close Event In Javascript For Firefox, IE and Chrome


这篇关于如何使用javascript在Firefox中获取浏览器关闭事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆