简体   繁体   English

Javascript窗口关闭事件而不是所有浏览器的卸载事件

[英]Javascript window close event rather than unload event for all browsers

I want to alert a user while the user tries to close the browser with out siginingoff or without saving some settings. 我想在用户尝试关闭浏览器而没有保存某些设置时提醒用户。

I am ding unload option in another page to alert unload data, but how can i alert a user on window.close(its not taking) 我在另一个页面中卸载选项以提醒卸载数据,但是如何在window.close上警告用户(它没有采取)

window.onbeforeunload = confirmExit;
function confirmExit(){
    if(readCookie("onlineVD") == "playing" && Confirm_Delete=="0")
    {
        return "You are leaving a video which is in play mode.Are you sure want to exit this page?";
    }
    else{
        Confirm_Delete="0";
    }
}

I want window.close for on tab close and on window close in all browsers. 我想在所有浏览器中关闭选项卡和窗口关闭window.close。

Please find me a solution 请找我一个解决方案

The event code you have already seems to work when I test it. 当我测试它时,你似乎已经使用了事件代码。 You just need to return false to stop the browser from closing. 您只需返回false即可阻止浏览器关闭。 The user will be asked if they're sure they want to navigate away from the page. 将询问用户他们是否确定要离开页面。

I'm using this shortened version of your code: 我正在使用这个缩短版的代码:

window.onbeforeunload = confirmExit;
function confirmExit(){
    alert("confirm exit is being called");
    return false;
}

The Mozilla documentation indicates that you should set the event.returnValue instead of simply returning a string: Mozilla文档表明您应该设置event.returnValue而不是简单地返回一个字符串:

window.onbeforeunload = confirmExit;
function confirmExit(e){
    if(readCookie("onlineVD") == "playing" && Confirm_Delete=="0")
    {
        var msg = "You are leaving a video which is in play mode.Are you sure want to exit this page?";
        if (e) {
            e.returnValue = msg;
        }

        return msg;
    }
    else{
        Confirm_Delete="0";
    }
}

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

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