最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript window close event rather than unload event for all browsers - Stack Overflow

programmeradmin1浏览0评论

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.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.

Please find me a solution

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.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.

Please find me a solution

Share Improve this question edited Jan 4, 2010 at 7:17 rahul 187k50 gold badges238 silver badges264 bronze badges asked Jan 4, 2010 at 7:14 ElamuruganElamurugan 3,21413 gold badges64 silver badges106 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 7

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. 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:

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";
    }
}
发布评论

评论列表(0)

  1. 暂无评论