65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.80/5 (17 votes)

Mar 5, 2011

CPOL
viewsIcon

149527

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