Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-25-2012, 04:49 AM   PM User | #1
iT3d.
New Coder

 
Join Date: May 2012
Posts: 45
Thanks: 7
Thanked 1 Time in 1 Post
iT3d. is an unknown quantity at this point
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?
iT3d. is offline   Reply With Quote
Old 05-25-2012, 05:33 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 05-25-2012, 01:24 PM   PM User | #3
iT3d.
New Coder

 
Join Date: May 2012
Posts: 45
Thanks: 7
Thanked 1 Time in 1 Post
iT3d. is an unknown quantity at this point
Code:
<body onbeforeunload="return confirmExit()">
Could I just modify this to call a confirm box rather than the confirmExit one used in the script?
iT3d. is offline   Reply With Quote
Old 05-25-2012, 07:55 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
iT3d. (06-01-2012)
Old 06-01-2012, 04:50 AM   PM User | #5
iT3d.
New Coder

 
Join Date: May 2012
Posts: 45
Thanks: 7
Thanked 1 Time in 1 Post
iT3d. is an unknown quantity at this point
Is there a way to disable the script activating when a page refreshes or the back button is pressed?
iT3d. is offline   Reply With Quote
Old 06-01-2012, 05:02 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 06-09-2012, 02:22 PM   PM User | #7
iT3d.
New Coder

 
Join Date: May 2012
Posts: 45
Thanks: 7
Thanked 1 Time in 1 Post
iT3d. is an unknown quantity at this point
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!
iT3d. is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:13 PM.


Advertisement
Log in to turn off these ads.