views:

500

answers:

2

Hi

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

+1  A: 

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";
    }
}
Rob Van Dam
Thank you for your reply @rob,Actually its working for tab close but here i need for window close also. When i close the window its not taking.
Ela
+1  A: 

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;
}
Richard
Thank you for your reply @richard,Actually its working for tab close but here i need for window close also. When i close the window its not taking.
Ela
I tested in Firefox 3.5 and it did, what browser are you using?
Richard
Also tested in IE8 and it works. (Added my test code example to my answer for you to test against)
Richard
I am using firefox 3.5.6 , the alert should not come when am going through any hiperlinks on the page or through form loading. But it shouyld come only on window .close and tab close. Its not working for me i tested in all the browsers of my office.
Ela
If you want to stop links on the page behaving that way, include extra JS on them that stops it, like this: http://www.pgrs.net/2008/1/30/popup-when-leaving-website
Richard