Jump to content

Javascript Redirect not working; But why?


mjurmann

Recommended Posts

Hello. So has proven to be the most pain in the butt problem I've run into in a long time.

 

I'm trying to setup a "Must be 18 years old or older to enter this site" splash page. The user is presented with two options: "Yes, I'm 18+" or "No, I'm younger than 18". If the user clicks the "Yes, I'm 18+" button, then the following function is triggered (this creates a cookie for the user so that they don't have to continue to see this page every time they visit):

 

<script type="text/javascript">
function saveSplash(domain) {
var expDate = new Date();
expDate.setTime(expDate.getTime()+(1*24*3600*1000*365));
setCookie("Age_Check", 1, expDate, '/', domain);
}

function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
        
        if (document.referrer != "") {
		alert ("TEST1");	
		<!--window.location = document.referrer;-->
	}
	else {
		alert ("TEST2");
		return false;	
		document.location="http://www.sitename.com/welcome.php";
	}
}
</script>

 

The cookie is created and saved correctly, however, after the cookie is saved, I need to redirect the user to the page they came from (document.referrer) or to the home page of the website if they have no referrer. The "TEST2" alert successfully pops up once the "Yes, I'm 18+" button has been clicked, however, the page seems to refresh instead of redirecting me to the http://www.sitename.com/welcome.php page.

 

I'm racking my brain over this one. I can't figure out why the redirect will not work and would really appreciate if someone could give me some help.

 

Thank you and have a nice evening.

Link to comment
Share on other sites

the code should be:

<script type="text/javascript">
function saveSplash(domain) {
var expDate = new Date();
expDate.setTime(expDate.getTime()+(1*24*3600*1000*365));
setCookie("Age_Check", 1, expDate, '/', domain);

window.location.href = document.referrer ? document.referrer : "http://www.sitename.com/welcome.php";
}

function setCookie(name, value, expires, path, domain, secure) {
document.cookie= name + "=" + escape(value) +
        	((expires) ? "; expires=" + expires.toGMTString() : "") +
        	((path) ? "; path=" + path : "") +
        	((domain) ? "; domain=" + domain : "") +
        	((secure) ? "; secure" : "");
}
</script>

Link to comment
Share on other sites

Unfortunately this did not work. I used exactly the same code but it still continues to redirect to that same page instead of the page I have specified.

 

Does anyone else have any idea why this won't redirect correctly? Here is the code I'm currently using:

 

<script type="text/javascript">
function saveSplash(domain) {
var expDate = new Date();
expDate.setTime(expDate.getTime()+(1*24*3600*1000*365));
setCookie("Age_Check", 1, expDate, '/', domain);
window.location.href = document.referrer ? document.referrer : "http://www.site.com/welcome.php";
}

function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
</script>

Link to comment
Share on other sites

First off I would like to explain what this means:

 

window.location.href = document.referrer ? document.referrer : "http://www.site.com/welcome.php";

 

This means that if there is a referrer, then go to referrer link, otherwise use http://www.site.com/welcome.php. 

 

Now I would do some debugging to see what is actually happening. 

 

Change:

 

window.location.href = document.referrer ? document.referrer : "http://www.site.com/welcome.php";

To

window.location.href = "http://www.site.com/welcome.php";

 

If you get redirected to http://www.site.com/welcome.php then document.referrer is the problem. 

 

My guess is that the document.referrer the same page, maybe you are redirecting to the same page in another part of your code?  Please see what referrer means as per http://msdn.microsoft.com/en-us/library/ms534365(VS.85).aspx:

 

This property returns a value only when the user reaches the current page through a link from the previous page. Otherwise, document.referrer returns an empty string; it also returns an empty string when the link is from a secure site.

 

For example, if PageA.htm includes a link to PageB.htm, and the user clicks that link, the document.referrer on PageB.htm returns "PageA.htm." However, if the user is on PageA.htm and types PageB.htm into the address line or chooses the Open command from the File menu to get to PageB.htm, the document.referrer returns an empty string.

 

 

 

Link to comment
Share on other sites

Alright, so I tried your advice and removed the referrer information. I've also included an Alert to trigger right before the redirect. The alert triggers, however, the redirect does not. It just reloads the current page, again. Here is the code I'm using:

 

<script type="text/javascript">
function saveSplash(domain) {
var expDate = new Date();
expDate.setTime(expDate.getTime()+(1*24*3600*1000*365));
setCookie("Age_Check", 1, expDate, '/', domain);
alert ("TEST");
window.location.href = "http://www.site.com/welcome.php";
}

function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

YAHOO.util.Event.addListener("enterbutton", "click", function() {
saveSplash(".site.com");
});

</script>

Link to comment
Share on other sites

I might as well include the FORM which the function is triggered by:

 

<form method="POST">
     <input id="enterbutton" type="submit" name="user_choice" value="Enter" /> <input type="submit" name="user_choice" value="Leave" />
</form>

 

This code triggers the function in the HEAD (I included this in the previous post):

 

<script type="text/javascript">
YAHOO.util.Event.addListener("enterbutton", "click", function() {
saveSplash(".site.com");
});
</script>

 

Hopefully this helps clarify something.

Link to comment
Share on other sites

In order to help further, we would need to see the rest of your code, however you may want to try a few other things:

 

If you are submitting through a form like:

 

<input type="submit" value="Over 18" onclick="saveSplash()" />  

 

Then change this to:

 

<input type="button" value="Over 18" onclick="saveSplash()" />  

 

OR

 

change your form tag to:

 

<form name="checkage" onsubmit="saveSplash(); return false;">

Link to comment
Share on other sites

Alright, the code which you provided worked, however, when using IE6 and IE7, document.referrer is NULL even when I click from a referring link to this page. document.referrer is correct in Firefox, Opera, Safari (both Mac and PC). IE6 and IE7 just seems to not record the document.referrer value.

 

Does anyone know an alternative to recording the referring link URL in IE6 and IE7? Here is the code I am using with the alert near the top which has a blank value in IE6 and IE7 but a correct value in all of the other browsers:

 

function saveSplash(domain) {
var expDate = new Date();
expDate.setTime(expDate.getTime()+(1*24*3600*1000*365));
setCookie("Age_Check", 1, expDate, '/', domain);
alert (document.referrer)
if (document.referrer != "") {
	window.location.href = document.referrer;
}
else {
	window.location.href="http://www.site.com/";
}
}

function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

</script>

 

 

Link to comment
Share on other sites

PHP should pick it up, so if this page is using PHP, you can use:

 

function saveSplash(domain) {
var expDate = new Date();
expDate.setTime(expDate.getTime()+(1*24*3600*1000*365));
setCookie("Age_Check", 1, expDate, '/', domain);

<?php
if($_SERVER['HTTP_REFERER']){ //I didn't spell referrer wrong, that is just how it is in PHP
  echo "	window.location.href='{$_SERVER['HTTP_REFERER']}';"
else
  echo "	window.location.href='http://www.site.com/';"
?>

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.