Click here to Skip to main content
15,867,594 members
Articles / Web Development / ASP.NET
Alternative
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.80/5 (20 votes)
7 Mar 2011CPOL 144.4K   13   26
Code...

Code


XML
<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)


Written By
Team Leader
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questiononbeforeunload fires on page refresh and url redirection Pin
Manas 19911-Jun-16 3:13
Manas 19911-Jun-16 3:13 
AnswerRe: onbeforeunload fires on page refresh and url redirection Pin
thatraja3-Jun-16 5:56
professionalthatraja3-Jun-16 5:56 
GeneralMy vote of 1 Pin
Er. Puneet Goel23-May-14 11:30
professionalEr. Puneet Goel23-May-14 11:30 
AnswerRe: My vote of 1 Pin
thatraja25-May-14 21:21
professionalthatraja25-May-14 21:21 
QuestionIt 's defected event onbeforeunload. Pin
Member 1054128619-Mar-14 20:17
Member 1054128619-Mar-14 20:17 
AnswerRe: It 's defected event onbeforeunload. Pin
thatraja25-May-14 21:21
professionalthatraja25-May-14 21:21 
Question[My vote of 1] This is not working anymore. Pin
Member 773936612-Jul-13 8:00
Member 773936612-Jul-13 8:00 
GeneralRe: Actually I got it what you said. BTW the requirement is just... Pin
thatraja14-Mar-11 16:11
professionalthatraja14-Mar-11 16:11 
GeneralRe: Yes I did notice the title. I was trying to convey that <cod... Pin
mgkr14-Mar-11 15:58
mgkr14-Mar-11 15:58 
GeneralReason for my vote of 5 Good job Pin
Ragunathan Maniraj14-Feb-12 19:13
Ragunathan Maniraj14-Feb-12 19:13 
GeneralReason for my vote of 1 This "solution" detects page reload ... Pin
pavel.hodek19-Sep-11 9:41
pavel.hodek19-Sep-11 9:41 
GeneralNice one. Pin
Ramalinga Koushik3-Jun-11 22:43
Ramalinga Koushik3-Jun-11 22:43 
GeneralReason for my vote of 3 I have given 3 as it is works only i... Pin
Uday Kumar B R14-Mar-11 17:35
Uday Kumar B R14-Mar-11 17:35 
GeneralRe: Oh dude, It works in 4 browsers(you can see that in Tip/Tric... Pin
thatraja15-Mar-11 6:54
professionalthatraja15-Mar-11 6:54 
GeneralLet me try with an analogy: Say you are asked to detect whe... Pin
mgkr14-Mar-11 16:38
mgkr14-Mar-11 16:38 
GeneralNo no no :-) It works on *anything* that "closes" the page -... Pin
mgkr14-Mar-11 10:22
mgkr14-Mar-11 10:22 
GeneralRe: Dude, I think you didn't noticed this Tip/Trick title 'How t... Pin
thatraja14-Mar-11 15:47
professionalthatraja14-Mar-11 15:47 
GeneralReason for my vote of 1 Works on other than just closing the... Pin
mgkr14-Mar-11 10:19
mgkr14-Mar-11 10:19 
GeneralReason for my vote of 5 Very good, my 5! Pin
Manfred Rudolf Bihy8-Mar-11 21:59
professionalManfred Rudolf Bihy8-Mar-11 21:59 
GeneralReason for my vote of 5 My vote is 5 Pin
Sunasara Imdadhusen7-Mar-11 21:25
professionalSunasara Imdadhusen7-Mar-11 21:25 
GeneralGood one!! Pin
Sunasara Imdadhusen7-Mar-11 21:25
professionalSunasara Imdadhusen7-Mar-11 21:25 
GeneralReason for my vote of 5 i was looking for this. Pin
Monjurul Habib5-Mar-11 7:28
professionalMonjurul Habib5-Mar-11 7:28 
GeneralReason for my vote of 5 Looks like an excellent solution, 5*... Pin
DrABELL5-Mar-11 3:59
DrABELL5-Mar-11 3:59 
GeneralRe: But opera still doesn't support the thing & we may expect th... Pin
thatraja7-Mar-11 13:24
professionalthatraja7-Mar-11 13:24 
GeneralReason for my vote of 5 good solution :) Pin
That's Aragon5-Mar-11 1:13
That's Aragon5-Mar-11 1:13 

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

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