...

View Full Version : Exit Prompt



gator93
05-17-2007, 11:22 PM
I have this code below:

<html>
<head>
<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)
window.location="http://www.yahoo.com"
</script>
</head>
<body>
Appalling material here
</body>
</html>

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?

Thanks.

Andrew

Philip M
05-18-2007, 08: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.

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

scrypter
05-18-2007, 11:04 AM
Not tested:

<script type="text/javascript">
window.onload=function() {
a=document.getElementsByTagName('a');
mysite=document.location.protocol+'//'+document.location.host+'/';
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 . . .'); }

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

Paul



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum