Geeks With Blogs

News

Microsoft MVP


DZone MVB


Moderator at CodeASP.NET


Quiz Master







free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

Some time ago, I always encountered questions in the forums including this thread and this thread on how execute codes when a user closes the browser.

AFAIK, the only events that you can use when the browser is closed is the onunload or the  onbeforeunload event.

The following code blocks below will simulates the closed button of the browser when the user invoked the button and display an alert message informing the user that the window is about to close..

Using the onunload event in the body element:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
        <script type=”text/javascript”>
            function doSomething(){
               alert('The Widnow is about to close');
            }
        </script>
</head>
<body onunload="doSomething();">
    <form id="form1" runat="server">
    </form>
</body
</html>
 
Using the onbeforeunload event:
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">
    <title>Untitled Page</title>
        <script type=”text/javascript”> 
           window.onbeforeunload = doSomething;

           function doSomething(){
               return "The window is about to close, Continue?";
           }    
        </script>   
</head>
<body>
    <form id="form1" runat="server">
    </form>
</body>

That's it! I Hope you will find this example useful!

 

Technorati Tags: ,,
Posted on Wednesday, May 20, 2009 6:24 PM ASP.NET , JavaScript , Tips&Tricks | Back to top


Comments on this post: FAQ: Capturing Browser Close Event

# re: FAQ: Capturing Browser Close Event
Requesting Gravatar...
Hi,

Another great article!

But how do I apply this in a scenerio where the user is yet to logout and attempting to close the window.

Thank you
Left by Emmao on Aug 12, 2009 6:05 AM

# re: FAQ: Capturing Browser Close Event
Requesting Gravatar...
Thank you it's works ^_^ Save me from searching
but, Can I make some calculation on rows before user close page, and restore the delete rows,
Well i was able to restore the delete rows through the datakays, but now am require to do the calculation before the closing the page,
and return the deleted rows and insert in database,
The idea is the rows should be equal 100 if in any case is user close page i want the deleted rows back to equal to 100..if not equal to 100.
I hope am clear
thank you so much
Left by Abbas on Aug 19, 2009 6:50 PM

# re: FAQ: Capturing Browser Close Event
Requesting Gravatar...
Dear Vinz, the box always appear in what ever i do somethig,
like i've have ddl with postback the it's appear in each time i pickup something, is there solution for , and sorrry for annyoing
Abbas
Left by Abbas on Aug 19, 2009 7:21 PM

# re: FAQ: Capturing Browser Close Event
Requesting Gravatar...
Turning off java script allows the visitor to escape with only being slightly miffed at the rude interuption when web browsing. In FF going to tools, options then clicking the appropriate box allows them to leave without having to do anything else with onunload or onbeforeunload event. This also allows the visitor to retain tabbed pages. Occasionally ctrl+alt+del becomes the other option to escape unscathed but results in closing the browser altogether.
Left by NG on May 26, 2011 1:18 AM

# re: FAQ: Capturing Browser Close Event
Requesting Gravatar...
But above line of code will work always
I need to show alert message when browser will close by X button in top most right corner
Left by Deepak on May 09, 2012 3:57 PM

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net