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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry OnBeforeUnload To Redirect User

    In my onbeforeunload event, it asked the user whether they want to leave the page or stay on it. When they click "stay on page", I want it to then redirect them to another webpage in the same window. Is there a way to do this, without just showing/hiding divs? Can I override the function? I'm currently trying to do it this way(as seen below) but now the dialog box is not showing up at all. It was working before Any ideas as to what I'm doing wrong, or how to accomplish this task?

    Code:
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>The Shocking Truth - Cabot Market Letter</title>
        
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function() {
                $("#vid").show();
                $("#div2").hide();
            });
            var test = 1;
            function manipulatetest()
            {
                test = 2;
            }
        window.onbeforeunload = onbeforeunload_Handler;
        function onbeforeunload_Handler()
        {       if (test == 1){
    
                    $("#vid").hide();
                    $("#div2").show();
                var confirm = confirm("Would you like to stay on this page?");
                if (confirm == true) {
                    window.location = "http://www.google.com";
                    }
                
            }
    }
    
    </script>
    </head>
    
    <style type="text/css"> 
    
    body {
    	background-color: #e0e6e5;
    }
    
    #vid {	
    	margin: 20px auto;
    	width: 920px;
    }
    
    </style>
    
    
    <body>
    
    <div id="vid">
    	<video width="920" height="540" autoplay preload controls>
    	  <source src="shockingtruth.mp4"  type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
    	  <source src="shockingtruth.webm" type='video/webm; codecs="vp8, vorbis"'>
    	  <source src="shockingtruth.ogv"  type='video/ogg; codecs="theora, vorbis"'>
    	  <object width="920" height="540" type="application/x-shockwave-flash"
    	    data="flowplayer-3.2.1.swf"> 
    	    <param name="movie" value="flowplayer-3.2.7.swf" /> 
    	    <param name="allowfullscreen" value="true" /> 
    	    <param name="flashvars" value='config={"clip": {"url": "http://www.cabot.net/videos/shocking-truth/shockingtruth.mp4", "autoPlay":true, "autoBuffering":true}}' /> 
    	    <p>Download video as <a href="pr6.mp4">MP4</a>, <a href="pr6.webm">WebM</a>, or <a href="pr6.ogv">Ogg</a>.</p> 
    	  </object>
    	</video>
    </div>
    
    <div id="div2">
    	<video width="920" height="540" autoplay preload controls>
    	  <source src="shockingtruth.mp4"  type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
    	  <source src="shockingtruth.webm" type='video/webm; codecs="vp8, vorbis"'>
    	  <source src="shockingtruth.ogv"  type='video/ogg; codecs="theora, vorbis"'>
    	  <object width="920" height="540" type="application/x-shockwave-flash"
    	    data="flowplayer-3.2.1.swf"> 
    	    <param name="movie" value="flowplayer-3.2.7.swf" /> 
    	    <param name="allowfullscreen" value="true" /> 
    	    <param name="flashvars" value='config={"clip": {"url": "http://www.cabot.net/videos/shocking-truth/shockingtruth.mp4", "autoPlay":true, "autoBuffering":true}}' /> 
    	    <p>Download video as <a href="pr6.mp4">MP4</a>, <a href="pr6.webm">WebM</a>, or <a href="pr6.ogv">Ogg</a>.</p> 
    	  </object>
    	</video>
    </div>
    
    <p style="text-align: center;"><a href="http://www.cabot.net/info/cml/cmlld03.aspx?source=ed01" onclick="manipulatetest();">Click Here To Order</a></p>
    
    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    onbeforeunload can only return one message as a string ... it will show this message and two buttons "Stay on page" / "Leave this page". It's the user's decision whether he wants to stay on page. It's not possible to intercept this.

  • #3
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is there another way I can accomplish this then? Simulate something very similar? When the user clicks "x", prompt them with two options? If they click one, close the window - if they click the other, show something different in the background?

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by taylor.alison View Post
    Is there another way I can accomplish this then? Simulate something very similar? When the user clicks "x", prompt them with two options? If they click one, close the window - if they click the other, show something different in the background?
    By short, no. Nor should be. As a user I would like to leave the site whenever I want, but if I am asked whether to stay and I confirm, i would like to stay, not to be redirected. Unexpected redirectings are annoying.

    Why do you need that?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,335
    Thanks
    11
    Thanked 588 Times in 569 Posts
    Quote Originally Posted by Kor View Post
    By short, no. Nor should be. As a user I would like to leave the site whenever I want, but if I am asked whether to stay and I confirm, i would like to stay, not to be redirected. Unexpected redirectings are annoying.

    Why do you need that?
    you have it backward kor: uf the user indicated to stay on the site, you can re-direct them. IF the OP has said they wanted to "redirect upon no", then you would be correct.


    OP: if you can get the onbefore unload to fire, you can setTimeout a re-direct inside. when the confirm box appears it freezes all execution, setTimeout included. if the user clicks leaves, your timeout is thrown away.
    if they stay, you timeout quickly takes them to the page you wanted to redirect to.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by rnd me View Post
    if they stay, you timeout quickly takes them to the page you wanted to redirect to.
    As I said, as a user I will hate that . There will no next time for me on that site
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Tags for this Thread

    Posting Permissions

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