Vinz' Blog (ProudMonkey)

"Code, Beer and Music ~ my way of being a programmer"
posts - 134, comments - 549, trackbacks - 0

My Links

News

Tag Cloud

Archives

Post Categories

Image Galleries

I'm a...

I'm at...

FAQ: Capturing Browser Close Event

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="form2" 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="form2" runat="server">

 

    </form>

</body>

 

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

 

Technorati Tags: ,,

Print | posted on Wednesday, May 20, 2009 6:24 PM | Filed Under [ ASP.NET JavaScript Tips&Tricks ]

Feedback

Gravatar

# re: FAQ: Capturing Browser Close Event

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
8/12/2009 6:05 AM | Emmao
Gravatar

# re: FAQ: Capturing Browser Close Event

@Emmao,

Try to refer to this threads:

http://forums.asp.net/t/1220828.aspx
http://forums.asp.net/p/1425261/3175122.aspx
8/12/2009 12:44 PM | Vinz
Gravatar

# re: FAQ: Capturing Browser Close Event

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
8/19/2009 6:50 PM | Abbas
Gravatar

# re: FAQ: Capturing Browser Close Event

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
8/19/2009 7:21 PM | Abbas
Gravatar

# re: FAQ: Capturing Browser Close Event

@Abbas,

Yes since onbeforeunload/unload will fire up when (closing, reloading, redirecting, etc.) in the page.

Take a look at this thread:
http://forums.asp.net/p/1094920/1658155.aspx
8/19/2009 8:06 PM | Vinz
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: