Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Exit Prompt

  1. #1
    New to the CF scene
    Join Date
    May 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exit Prompt

    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

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,298
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    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.

  • #3
    Regular Coder
    Join Date
    May 2007
    Posts
    118
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Display whatever when exiting from page is what most people hate I think
    Forum for webmasters and developers
    http://www.htmlfrenzy.com
    Are you an IT expert? Join our Reward system
    www.htmlfrenzy.com/reward-system.php

  • #4
    New Coder
    Join Date
    Sep 2005
    Location
    New Zealand
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    try this code

    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
    ScrypTik Javascript editor with built in syntax error checking


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •