Geeks With Blogs

News Locations of visitors to this page

Post Categories

hmloo's World of .NET

All modern browsers support the JavaScript's  window.onbeforeunload event. It fires before the unload event when the page is unloaded. So this event always uses to stop a page from exit. The following codes illustrate how to implement this function:

window.onbeforeunload = function (e) {
   var message = 'Are you sure you want to leave?'; 
    var e = e || window.event;
    // For IE and Firefox prior to version 4
    if (e) {
        e.returnValue = message ;
    }
    // For Safari
    return message;
};

and we can also use this event to disable button before postback is done or form is submitted so that user cannot click button again. It is very simple.

window.onbeforeunload = function () { 
  document.getElementById("<%=Button1.ClientID %>").disabled = true; 
};
Posted on Wednesday, February 15, 2012 9:51 PM | Back to top


Comments on this post: Use window.onbeforeunload event to stop browser from closing or Disable Button before Page PostBack

# re: Use window.onbeforeunload event to stop browser from closing or Disable Button before Page PostBack
Requesting Gravatar...
This is really fantastic advice, thank you so much
Left by redsnow on Feb 15, 2012 11:52 PM

# re: Use window.onbeforeunload event to stop browser from closing or Disable Button before Page PostBack
Requesting Gravatar...
Hi,

The buttons are disabled after using the below code:-

window.onbeforeunload = function () {
document.getElementById("<%=Button1.ClientID %>").disabled = true;
};


How do ienable the buttons if a user clicks cancel.
Left by Syed on May 04, 2012 3:40 AM

# re: Use window.onbeforeunload event to stop browser from closing or Disable Button before Page PostBack
Requesting Gravatar...
I am using the onbeforeunload method for my form in jquery.
But it is showing different out come for different browsers.
Can anybody help me with any alternative which gives the same customized message in all browsers of different versions.

Thanks in Advance,
Deepak
Left by Deepak on Jun 06, 2012 10:27 PM

# re: Use window.onbeforeunload event to stop browser from closing or Disable Button before Page PostBack
Requesting Gravatar...
Then you can use this to block the idiots that prevent navigation by a user - when someone clicks the X.. they want to leave your page. forcing them to remain is LAME! - suck it up buttercup

// ==/UserScript==

var th = document.getElementsByTagName('body')[0];
var s = document.createElement('script');
s.setAttribute('type','text/javascript');
s.innerHTML = "window.onbeforeunload = function() {}";
th.appendChild(s);
Left by analyzes on Jan 15, 2014 3:41 PM

# re: Use window.onbeforeunload event to stop browser from closing or Disable Button before Page PostBack
Requesting Gravatar...
dfsav rfsdvcx hgbn jmnb ertg
Left by sfdd on May 19, 2014 9:51 PM

# re: Use window.onbeforeunload event to stop browser from closing or Disable Button before Page PostBack
Requesting Gravatar...
Comment: *I do want to hear your thoughts. Please enter a comment.Allowed tags: blockquote, a, strong, em, p, u, strike, super, sub, codeComment: Allowed tags: blockquote, a, strong, em, p, u, strike, super, sub, code
Left by Name: *And who are you? on Feb 20, 2017 8:48 PM

Your comment:
 (will show your gravatar)


Copyright © hmloo | Powered by: GeeksWithBlogs.net