Click here to Skip to main content
15,665,480 members
Please Sign up or sign in to vote.
4.50/5 (3 votes)
See more:
hi to all
i want to show a alert to users after close browser window.

you may say that use of unload or onunload event
but this event fire when my submit button in my form is clicked
and this alert show to them when form is submitted!!!!!
i want to show alert just close from browser.
how can i do this
thanks for any help
Updated 11-Mar-14 5:01am

1 solution

you can use following script as 

var confirmOnPageExit = function (e) {
     // If we haven't been passed the event get the window.event
     e = e || window.event;

     var message = 'Your information will be lost. Are you sure ?';

     // For IE6-8 and Firefox prior to version 4
     if (e) {
         e.returnValue = message;

     // For Chrome, Safari, IE8+ and Opera 12+
     return message;

 // Turn it on - assign the function that returns the string
 window.onbeforeunload = confirmOnPageExit;

DeActivate on Submit button

<input type="submit" onclick="window.onbeforeunload = null; " value="Save"  />

For the new changes in input fields

$('input').blur(function () {

      window.onbeforeunload = confirmOnPageExit;
Share this answer
mhd.sbt 11-Mar-14 15:39pm    
thanks for your answer.
this way just work for the show an alert
is there a way that use this way for call a public method in "confirmonpageExit" function
for example call a method like below in this function :
i want to call a public method when user click the close button in browser.
thanks for your time.

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