You are here: irt.org | FAQ | JavaScript | Document | Q370 [ previous next ]
Once a user has chosen to unload a document apart from telling him/her that he/she is leaving there's not much that can be done.
If however you are only interested in whether the user clicks a link on the page then you can confirm that they want to leave:
<SCRIPT LANGUAGE="JavaScript"><!-- function query(url) { if (confirm("Do ya really wanna go?")) location.href = url } //--></SCRIPT> <A HREF="javascript:query('../home.html')">Home Page</A>
However, the user can: close the window, close the browser, press back, type in a url, select a url from the history, select a url from the bookmarks - none of which can be prevented.
The following was submitted by wEEdpEckEr
Its actually pretty simple, instead of the onLoad, just use the onUnload attribute for the body-tag. When the users closes the page, goes to another one or anything else that leads away from that page, the function will be executed.
<body onUnload="alert('Goodbye!')">