Click here to Skip to main content
12,254,932 members (60,995 online)
Click here to Skip to main content
Add your own
alternative version


12 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
Retired from Q/A section & Technical forums since 2014

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

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