Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap

Q1791 How can I detect the browser close event?

You are here: irt.org | FAQ | JavaScript | Window | Q1791 [ previous next ]

Here is something that work for IE4 and up.

In the onUnload event of the html <BODY> tag, call a function that used the mouse position to determine if the browser close button has been click. The clientX and clientY properties retrieves negative values since they excluded window decorations and scroll bars.

<script language="JavaScript"><!—
function unloadPage() {
  if (window.event) {
    if (window.event.clientX < 0 && window.event.clientY < 0) {
      //  do something
      alert('Detecting browser close event');

<body class="Plain" onUnload="unloadPage()">

Submitted by CGagnon

Feedback on 'Q1791 How can I detect the browser close event?'

©2018 Martin Webb