Click here to Skip to main content
15,891,909 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I know there are a lot of questions regarding this but nothing is answering me right. I want to show a confirmation dialog when user leaves the page. If the user press Cancel he will stay on page and if OK the changes that he has made will be rollback-ed by calling a method. I have done like this:

JavaScript
var _AreyousureLeaving = "Are you sure you want to leave?";
window.onbeforeunload = function (evt) {
                var message = _AreyousureLeaving;
                  if (typeof evt == 'undefined') {
                    evt = window.event;
                }
                if (evt) {
                    confirm(_AreyousureLeaving);
                    evt.returnValue = message;
                }
                return message;  
        }


This code is working fine in IE and Firefox. But this code is not working in Chrome. the statements confirm(_AreyousureLeaving); is not working.

if there is any way to control this popupt? Thanks
Posted
Updated 13-Nov-20 18:43pm
v2
Comments
Robert Welliever 27-Oct-14 3:30am    
I don't know why you ignored my correct answer, but Chrome is properly blocking your confirm dialog exactly as it is designed to. Do you just not believe that is the case? I gave you instructions to verify for yourself and the solution to your problem.

Hi,

Please refer below code its wroking fine with I.E, Mozila Firefox and chrome..

XML
<script type="text/javascript" >
        window.onbeforeunload = function () {
            return "Do you want to leave?"
        }

        // A jQuery event (I think), which is triggered after "onbeforeunload"
        $(window).unload(function () {
            alert("Do Reset..");
            //I will call my method
        });
   </script>




Thanks
Zubair
 
Share this answer
 
Comments
Osama Abu Sitta 27-Oct-14 1:27am    
I know this code working but I want to create confirm message with ok /cancel and
If the user press Cancel he will stay on page and if OK I want to do function before leaving the page
Thanks !
/\jmot 27-Oct-14 7:15am    
<script type="text/javascript" >
window.onbeforeunload = function () {
return "Do you want to leave?"
}

// A jQuery event (I think), which is triggered after "onbeforeunload"
$(window).unload(function () {
if(Confirm("Do Reset..??"))
{
alert("You Press OK");
}
else
{
alert("You Press CANCEL");
}
//I will call my method
});
</script>
Do you have '_AreyousureLeaving' defined in a global scope? Your code looks a little wonky using local variable 'message' that doesn't serve a purpose and you're using the deprecated event.returnValue. However, I had a similar issue and discovered Chrome was blocking alert and confirm dialogs automatically in the onunload event. I never traced through the jQuery source to see how they resolved the issue, but if you use jQuery it should solve your problem. Like this:

$(window).on('beforeunload', function () { return 'Please Stay On This Page'; });

Good luck.
 
Share this answer
 
Comments
Osama Abu Sitta 27-Oct-14 1:39am    
I have defined _AreyousureLeaving in a global scope
the error occur at the confirm message just in chrome
Robert Welliever 27-Oct-14 2:03am    
If you right click Chrome when running, then 'Inspect Element', click the 'Resources' tab, click 'Frames' and navigate to where your event should execute, you will see Chrome block it and throw an error. You can't prevent that from happening so you have to send the popup before onunload is fired. jQuery's beforeunload fires before onunload, but has pretty much the same effect. If you put your _AreyousureLeaving variable right where I put 'Please Stay On This Page' in the code snippet above, it should work as you intend.
$(window).on('beforeunload', function () { return 'Please Stay On This Page'; });
I am using the code but default msg alert only display like"Changes you made may not be saved". Refresh my page that time how to display my own comments display alert in my browser.
 
Share this answer
 
Comments
Richard Deeming 6-Oct-17 15:01pm    
In what way is this supposed to be a "solution" to the question?!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900