View Full Version : Exit Prompt

05-18-2007, 12:22 AM
I have this code below:

<script type="text/javascript">
var stay=confirm("The following site contains appalling material suitable only for webmasters. Please 'ok' to enter, 'cancel' to exit immediately!")
if (!stay)
Appalling material here

Which works great. But I'm wanting this window to come up only when someone clicks away from the site or exits. Basically using this along with the onBeforeUnload function in the body tag.

How can I make this come up only when exiting not when entering a page?



Philip M
05-18-2007, 09:17 AM
You can use onbeforeunload (not supported by all browsers) to display a pre-exit message or confirm box, but you cannot redirect the current window or open a modal dialog from the current window in the onbeforeunload event - if that was possible the internet would be unuseable.

05-18-2007, 10:04 AM
Display whatever when exiting from page is what most people hate I think :)

05-18-2007, 12:04 PM
Not tested:

<script type="text/javascript">
window.onload=function() {
for (i=0; i<a.length; i++)
if (a[i].href.substr(0, 4)=='http' && a[i].href.substr(0, mysite.length)!=mysite)
a[i].onclick=function() { return confirm('The following site contains appalling material . . .'); }

Assumes all exits are links, if there are button you would have to deal with them too.
Put code in head of each page.