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 7 of 7
  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    45
    Thanks
    7
    Thanked 1 Time in 1 Post

    Help Building on a Exit Splash Alternative

    Here's the base code:
    Code:
    <script type="text/javascript">
    var exit=true;
        function confirmExit()
        {
        if(exit)
        location.assign('special.html');
        if(exit)
        return "Message Goes Here";
        }
    </script>
    But it doesn't do exactly what I need it to

    What I need (and think is possible, I'm just missing how to do it) is to use this script to make sure they're leaving the site (there's more to the script [that is placed on links] to declare what will not trigger it) But instead of returning to the viewer on attempted exit this 'leave or stay on the page' dialog, I think this script would be better if it would return with a regular confirm box - which will display a message and offer to redirect them to the page I want them at using a OK to redirect them, and cancel to just leave the page however they intended.

    I tried just playing with the original script but found that Google Chrome and IE would do different things when trying to follow it - which is why I want this to launch a confirm box which I can link a straightforward redirect to.

    Any help?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    And WHEN is your confirmExit function called?? Via window.onunload??

    If so, you can't change the location at unload time. It's a browser safety feature to prevent sites from holding users captive.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    May 2012
    Posts
    45
    Thanks
    7
    Thanked 1 Time in 1 Post
    Code:
    <body onbeforeunload="return confirmExit()">
    Could I just modify this to call a confirm box rather than the confirmExit one used in the script?

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    No. onbeforeunload has VERY strict rules. It can be used in one way and one way only.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    iT3d. (06-01-2012)

  • #5
    New Coder
    Join Date
    May 2012
    Posts
    45
    Thanks
    7
    Thanked 1 Time in 1 Post
    Is there a way to disable the script activating when a page refreshes or the back button is pressed?

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Disable what script? The onbeforeunload? Yes, easy.

    Code:
    <script>
    function closeIt()
    {
        if ( youDoNotWantTheDialogToAppear ) return false;
    
        return "Your message asking them to stay on the page';
    }
    window.onbeforeunload = closeIt;
    </script>
    Of course, now you have to figure out HOW or WHY they are leaving the page and set that youDoNotWantTheDialogToAppear value appropriately.

    So in your case, you'd have to figure out they were doing BACK or refresh and return false. Not sure how you want to do that.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New Coder
    Join Date
    May 2012
    Posts
    45
    Thanks
    7
    Thanked 1 Time in 1 Post
    Ah. Well I happened upon a spell of luck and came across a website that had the actual script installed - and not called externally so I was able to copy it. I think if I could tweak it to not set off the function controlling exit actions on the refresh button - it would be perfect!


  •  

    Posting Permissions

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