Click here to Skip to main content
Click here to Skip to main content
Alternative Tip/Trick

How to detect browser close when browser [X] is clicked ?

, 7 Mar 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Code...

Code

<html>
<head><title>Detect Browser Exit</title>
<script type="text/javascript" language="Javascript">
 
function DetectBrowserExit()
{
   alert('Execute task which do you want before exit');
}
 
window.onbeforeunload = function(){ DetectBrowserExit(); }
 
</script>
</head>
<body>
Detect Browser Exit
</body>
</html>
 

jQuery

I found the code in jQuery. Clickety[^]

Browser Compatibility

I have tested this script in the following Web browsers:
  • Internet Explorer
  • Mozilla Firefox
  • Google Chrome
  • Safari

License

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

Share

About the Author

thatraja
Web Developer
India India
Programmer | Dude | ||||||
 
About him & his blog? check out here. Currently he's updating the site, he'll return soon there with more things*.
 
In Codeproject, he posted a blogpost & more than couple of Tip/Tricks, hope he'll write more soon.
 
Favorite techs are MVC, WCF, Mobile, HTML5, CSS3, jQuery, etc.,
 
His hobbies are watching movies, CP, being alone, etc., But now he's working on something*.
 
* - SCAMP???
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralCode to Detect Browser Close Using jQuery In Asp.Net PinmemberBinu198527-Jun-14 2:23 
GeneralMy vote of 1 PinprofessionalEr. Puneet Goel23-May-14 12:30 
AnswerRe: My vote of 1 Pinprotectorthatraja25-May-14 22:21 
QuestionIt 's defected event onbeforeunload. PinmemberMember 1054128619-Mar-14 21:17 
AnswerRe: It 's defected event onbeforeunload. Pinprotectorthatraja25-May-14 22:21 
Question[My vote of 1] This is not working anymore. PinmemberMember 773936612-Jul-13 9:00 
GeneralRe: Actually I got it what you said. BTW the requirement is just... Pinmvpthatraja14-Mar-11 17:11 
GeneralRe: Yes I did notice the title. I was trying to convey that <cod... Pinmembermgkr14-Mar-11 16:58 
GeneralReason for my vote of 5 Good job PinmemberRagunathan Maniraj14-Feb-12 20:13 
GeneralReason for my vote of 1 This "solution" detects page reload ... Pinmemberpavel.hodek19-Sep-11 10:41 
GeneralNice one. PinmemberRamalinga Koushik3-Jun-11 23:43 
GeneralReason for my vote of 3 I have given 3 as it is works only i... PinmemberUday Kumar B R14-Mar-11 18:35 
GeneralRe: Oh dude, It works in 4 browsers(you can see that in Tip/Tric... Pinmvpthatraja15-Mar-11 7:54 
GeneralLet me try with an analogy: Say you are asked to detect whe... Pinmembermgkr14-Mar-11 17:38 
GeneralNo no no :-) It works on *anything* that "closes" the page -... Pinmembermgkr14-Mar-11 11:22 
GeneralRe: Dude, I think you didn't noticed this Tip/Trick title 'How t... Pinmvpthatraja14-Mar-11 16:47 
GeneralReason for my vote of 1 Works on other than just closing the... Pinmembermgkr14-Mar-11 11:19 
GeneralReason for my vote of 5 Very good, my 5! PinmvpManfred R. Bihy8-Mar-11 22:59 
GeneralReason for my vote of 5 My vote is 5 PinmemberSunasara Imdadhusen7-Mar-11 22:25 
GeneralGood one!! PinmemberSunasara Imdadhusen7-Mar-11 22:25 
GeneralReason for my vote of 5 i was looking for this. PinmemberMonjurul Habib5-Mar-11 8:28 
GeneralReason for my vote of 5 Looks like an excellent solution, 5*... PinmemberDrABELL5-Mar-11 4:59 
GeneralRe: But opera still doesn't support the thing & we may expect th... Pinmvpthatraja7-Mar-11 14:24 
GeneralReason for my vote of 5 good solution :) PinmemberThat's Aragon5-Mar-11 2:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 7 Mar 2011
Article Copyright 2011 by thatraja
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid