简体   繁体   English

浏览器关闭事件不是页面刷新/重新加载事件

[英]browser close event not page refresh/reload event

i need to detect the browser close event only not page refresh/reload event我只需要检测浏览器关闭事件而不是页面刷新/重新加载事件

I have tried this , but in both condition its working我试过这个,但在这两种情况下它都可以工作

    $(window).bind('beforeunload',function(){

        });

 window.onbeforeunload = function (e) {

        }
$(window).unload(function() {  

});​

There is no specific method for capturing browser Close Event .没有捕获浏览器Close Event特定方法。

You can only capture on unload of the current page.您只能在unload当前页面时捕获。

Which also works when you try to refresh or reload the page.当您尝试refreshreload页面时,这也有效。

You could try with setting some value that checks if page was allready loaded and only then trigger onbeforeunload.您可以尝试设置一些值来检查页面是否已加载完毕,然后才触发 onbeforeunload。

I was using something similar:我正在使用类似的东西:

window.onbeforeunload = function (e) {
            if(is_form_dirty())
                return '<@msg "elections.general.modal.confirm.changes.text" />';
        }

Setting should be "session" based - so if possible use localstorage or just plain old cookies设置应该基于“会话” - 所以如果可能的话,使用 localstorage 或只是普通的旧 cookie

There is no separate event for reload or close.没有用于重新加载或关闭的单独事件。 Browser fires the same bodyunload event for either of the case.浏览器为任何一种情况触发相同的 bodyunload 事件。

If you have to detect the close desparately, you can do by checking the coordinates from window.event.clientX and window.event.clientY.如果你必须检测到关闭,你可以通过检查 window.event.clientX 和 window.event.clientY 的坐标来完成。 But this is the last thing you would want to do.但这是您最不想做的事情。

If you are using IE only, you can implement BHO that might detect the browser close explicitly.如果您只使用 IE,您可以实现 BHO,它可能会显式检测浏览器关闭。

i did it like that;我就是这样做的;

    <script>

var mie = (navigator.appName == "Microsoft Internet Explorer") ? true : false;

if (!mie) {
     document.captureEvents(Event.MOUSEMOVE);
     document.captureEvents(Event.MOUSEDOWN);
}

document.onmousemove = function (e) {mousePos(e);};
document.onmousedown = function (e) {mouseClicked();};

var mouseClick;
var keyClicked;

var mouseX = 0;
var mouseY = 0;

function mousePos (e) {
    if (!mie) {
        mouseX = e.pageX; 
        mouseY = e.pageY;
    }
    else {
        mouseX = event.clientX + document.body.scrollLeft;
        mouseY = event.clientY + document.body.scrollTop;
    }

    document.show.mouseXField.value = mouseX;
    document.show.mouseYField.value = mouseY;

    return true;
}

window.onbeforeunload = function (e)
        {
            e = e || window.event;
            var y = e.pageY || e.clientY;
            if (y <= 0){    

            }
            else {
                if(document.show.mouseYField.value < 30) {
                    return 'Do you want to exit?';
                }
            }
          }
</script>

<form name ="show" style="display:none">
        <input type="text" name="mouseXField" value="0" size="6">Mouse X Position<br>
        <input type="text" name="mouseYField" value="0" size="6">Mouse Y Position<br>
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM