简体   繁体   中英

Show alert message on chrome similar to firefox on window close.Confirmation message “Leave” or “Stay on this page” not required

I want to show an alert message before window close of webpage.The below code is working fine for firefox and IE but it is not showing the alert message on chrome and safari.

**window.onbeforeunload=uEvent;
function uEvent()
{
  alert("you are being logged out");
}**

For chrome,I tried using return "you are being logged out"; in place of alert message but this alert message gives user option to "Leave this page" or "stay on this page" which i dont want in my case. Please help me out so that chrome and safari have also the similar behavior like firefox and IE

Thanks to Youri Arkesteijn , you can use this code:

var dont_confirm_leave = 0; //set dont_confirm_leave to 1 when you want the user to be able to leave withou confirmation
        var leave_message = 'You sure you want to leave?'
        function goodbye(e) 
        {
            if(dont_confirm_leave!==1)
            {
                if(!e) e = window.event;
                //e.cancelBubble is supported by IE - this will kill the bubbling process.
                e.cancelBubble = true;
                e.returnValue = leave_message;
                //e.stopPropagation works in Firefox.
                if (e.stopPropagation) 
                {
                    e.stopPropagation();
                    e.preventDefault();
                }

                //return works for Chrome and Safari
                return leave_message;
            }
        }   
    window.onbeforeunload=goodbye;  

you can see this question ... on stackoverflow..
I checked this in chrome , IE and Mozila ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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