Click here to Skip to main content
11,929,675 members (58,433 online)
Click here to Skip to main content
Add your own
alternative version


11 bookmarked

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

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


<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(); }
Detect Browser Exit


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


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


About the Author

Web Developer
India India
Coming soon....

You may also be interested in...

Comments and Discussions

GeneralCode to Detect Browser Close Using jQuery In Asp.Net Pin
Binu198527-Jun-14 2:23
memberBinu198527-Jun-14 2:23 
GeneralMy vote of 1 Pin
Er. Puneet Goel23-May-14 12:30
professionalEr. Puneet Goel23-May-14 12:30 
AnswerRe: My vote of 1 Pin
thatraja25-May-14 22:21
protectorthatraja25-May-14 22:21 
QuestionIt 's defected event onbeforeunload. Pin
Member 1054128619-Mar-14 21:17
memberMember 1054128619-Mar-14 21:17 
AnswerRe: It 's defected event onbeforeunload. Pin
thatraja25-May-14 22:21
protectorthatraja25-May-14 22:21 
Question[My vote of 1] This is not working anymore. Pin
Member 773936612-Jul-13 9:00
memberMember 773936612-Jul-13 9:00 
GeneralRe: Actually I got it what you said. BTW the requirement is just... Pin
thatraja14-Mar-11 17:11
mvpthatraja14-Mar-11 17:11 
GeneralRe: Yes I did notice the title. I was trying to convey that <cod... Pin
mgkr14-Mar-11 16:58
membermgkr14-Mar-11 16:58 
GeneralReason for my vote of 5 Good job Pin
Ragunathan Maniraj14-Feb-12 20:13
memberRagunathan Maniraj14-Feb-12 20:13 
GeneralReason for my vote of 1 This "solution" detects page reload ... Pin
pavel.hodek19-Sep-11 10:41
memberpavel.hodek19-Sep-11 10:41 
GeneralNice one. Pin
Ramalinga Koushik3-Jun-11 23:43
memberRamalinga Koushik3-Jun-11 23:43 
GeneralReason for my vote of 3 I have given 3 as it is works only i... Pin
Uday Kumar B R14-Mar-11 18:35
memberUday Kumar B R14-Mar-11 18:35 
GeneralRe: Oh dude, It works in 4 browsers(you can see that in Tip/Tric... Pin
thatraja15-Mar-11 7:54
mvpthatraja15-Mar-11 7:54 
GeneralLet me try with an analogy: Say you are asked to detect whe... Pin
mgkr14-Mar-11 17:38
membermgkr14-Mar-11 17:38 
GeneralNo no no :-) It works on *anything* that "closes" the page -... Pin
mgkr14-Mar-11 11:22
membermgkr14-Mar-11 11:22 
GeneralRe: Dude, I think you didn't noticed this Tip/Trick title 'How t... Pin
thatraja14-Mar-11 16:47
mvpthatraja14-Mar-11 16:47 
GeneralReason for my vote of 1 Works on other than just closing the... Pin
mgkr14-Mar-11 11:19
membermgkr14-Mar-11 11:19 
GeneralReason for my vote of 5 Very good, my 5! Pin
Manfred R. Bihy8-Mar-11 22:59
mvpManfred R. Bihy8-Mar-11 22:59 
GeneralReason for my vote of 5 My vote is 5 Pin
Sunasara Imdadhusen7-Mar-11 22:25
memberSunasara Imdadhusen7-Mar-11 22:25 
GeneralGood one!! Pin
Sunasara Imdadhusen7-Mar-11 22:25
memberSunasara Imdadhusen7-Mar-11 22:25 
GeneralReason for my vote of 5 i was looking for this. Pin
Monjurul Habib5-Mar-11 8:28
memberMonjurul Habib5-Mar-11 8:28 
GeneralReason for my vote of 5 Looks like an excellent solution, 5*... Pin
DrABELL5-Mar-11 4:59
memberDrABELL5-Mar-11 4:59 
GeneralRe: But opera still doesn't support the thing & we may expect th... Pin
thatraja7-Mar-11 14:24
mvpthatraja7-Mar-11 14:24 
GeneralReason for my vote of 5 good solution :) Pin
That's Aragon5-Mar-11 2:13
memberThat's Aragon5-Mar-11 2: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.

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