11 Replies - 10022 Views - Last Post: 08 July 2011 - 11:21 AM

#1 taylor.alison  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 14-October 10

OnBeforeUnload to redirect user

Posted 08 July 2011 - 10:04 AM

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?
Is This A Good Question/Topic? 0
  • +

Replies To: OnBeforeUnload to redirect user

#2 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 472
  • Joined: 27-June 10

Re: OnBeforeUnload to redirect user

Posted 08 July 2011 - 10:09 AM

First: I love how you give them the option of staying, and then if they choose to stay, you redirect them, xD

And did you set it up via a "confirm" page?
Like:
var confirm = confirm("Would you like to stay on the page?");


Because if so, just type something like:
if (confirm == true) {
     window.location = "URL";
}


Was This Post Helpful? 1
  • +
  • -

#3 taylor.alison  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 14-October 10

Re: OnBeforeUnload to redirect user

Posted 08 July 2011 - 10:13 AM

View Postmaniacalsounds, on 08 July 2011 - 10:09 AM, said:

First: I love how you give them the option of staying, and then if they choose to stay, you redirect them, xD

And did you set it up via a "confirm" page?
Like:
var confirm = confirm("Would you like to stay on the page?");


Because if so, just type something like:
if (confirm == true) {
     window.location = "URL";
}



You know, I thought it was weird too. It's something for work - I didn't really want to question them...I almost did, and then I backspaced and decided not to. Meh. I'll give it a shot. Right now all that is happening is this: (it was showing/displaying a different div depending on the selection, but now it's going to redirect them - if it works!)

 window.onbeforeunload = onbeforeunload_Handler;
    function onbeforeunload_Handler()
    {
            if (test == 1)
            {
                $("#vid").hide();
                $("#div2").show();
                return "Dialog Box pop up";
                }
    }

Was This Post Helpful? 0
  • +
  • -

#4 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 472
  • Joined: 27-June 10

Re: OnBeforeUnload to redirect user

Posted 08 July 2011 - 10:17 AM

Hahaha, xD Your post might've made my day.

What does the variable test represent? Could we see your whole code, please?
Was This Post Helpful? 0
  • +
  • -

#5 taylor.alison  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 14-October 10

Re: OnBeforeUnload to redirect user

Posted 08 July 2011 - 10:18 AM

Glad I could be of some assistance!

<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;
        });
        var test = 1;
        function manipulatetest()
        {
            test = 2;
        }
    window.onbeforeunload = onbeforeunload_Handler;
    function onbeforeunload_Handler()
    {
            if (test == 1)
            {
                $("#vid").hide();
                $("#div2").show();
                return "Dialog Box pop up";
                }
    }
</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>

Was This Post Helpful? 0
  • +
  • -

#6 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 472
  • Joined: 27-June 10

Re: OnBeforeUnload to redirect user

Posted 08 July 2011 - 10:22 AM

I didn't mean ALL of the code, just the Javascript, but oh well, even better :)

First: line 12 is unnecessary, because two lines later, you set it to the same value :)

Second: You don't have the thingy popup to ask them if they want to stay.

Let's edit your function as such:
    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 = "THENEWURL";
                }
            }
    }


Was This Post Helpful? 0
  • +
  • -

#7 taylor.alison  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 14-October 10

Re: OnBeforeUnload to redirect user

Posted 08 July 2011 - 10:31 AM

Hmm, unfortunately nothing pops up - the window just closes. If I run it in debugger mode, I get an error in some Global.asax file saying "Object reference not set to an instance of an object" on "Response.Write(err.InnerException.Message);

does this have anything to do with my code?
Was This Post Helpful? 0
  • +
  • -

#8 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 472
  • Joined: 27-June 10

Re: OnBeforeUnload to redirect user

Posted 08 July 2011 - 10:33 AM

I don't think so.... It looks more like an ASP.NET error. Sorry, I have no experience in ASP.NET, only PHP.

BTW, I forgot a semicolon on line 8 of my code... maybe that helps the ASP.NET parse or something?
Was This Post Helpful? 0
  • +
  • -

#9 taylor.alison  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 14-October 10

Re: OnBeforeUnload to redirect user

Posted 08 July 2011 - 10:39 AM

Ah. No luck :( I don't understand why nothing is showing at all...
Was This Post Helpful? 0
  • +
  • -

#10 taylor.alison  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 14-October 10

Re: OnBeforeUnload to redirect user

Posted 08 July 2011 - 10:51 AM

I even tried taking out the if statement, but I still had no luck :/
Was This Post Helpful? 0
  • +
  • -

#11 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 472
  • Joined: 27-June 10

Re: OnBeforeUnload to redirect user

Posted 08 July 2011 - 10:59 AM

Apparently, the onbeforeunload will only execute return statements on most browsers. And that another problem - not every browser will do that. I don't think there's a full proof way to make it cross-browser with the confirm() menu.

I believe this link might help, it seems (minutely) cross browser:
http://www.openjs.co...onfirmation.php
Was This Post Helpful? 0
  • +
  • -

#12 taylor.alison  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 14-October 10

Re: OnBeforeUnload to redirect user

Posted 08 July 2011 - 11:21 AM

I can't tell what is going on. Before, I was definitely getting a dialog box asking whether or not I wanted to stay. It was working on chrome and IE. Then it was only working on IE. I didn't test Firefox. Now why is it all of a sudden working on neither @.@
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1