mjurmann Posted June 25, 2008 Share Posted June 25, 2008 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. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted June 26, 2008 Share Posted June 26, 2008 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> Quote Link to comment Share on other sites More sharing options...
mjurmann Posted June 26, 2008 Author Share Posted June 26, 2008 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> Quote Link to comment Share on other sites More sharing options...
fanfavorite Posted June 26, 2008 Share Posted June 26, 2008 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. Quote Link to comment Share on other sites More sharing options...
mjurmann Posted June 26, 2008 Author Share Posted June 26, 2008 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> Quote Link to comment Share on other sites More sharing options...
mjurmann Posted June 26, 2008 Author Share Posted June 26, 2008 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. Quote Link to comment Share on other sites More sharing options...
fanfavorite Posted June 26, 2008 Share Posted June 26, 2008 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;"> Quote Link to comment Share on other sites More sharing options...
fanfavorite Posted June 26, 2008 Share Posted June 26, 2008 Ok try changing: <form method="POST"> TO <form name="checkage" onsubmit="saveSplash('.site.com'); return false;"> Quote Link to comment Share on other sites More sharing options...
fanfavorite Posted June 26, 2008 Share Posted June 26, 2008 Ok try changing: <form method="POST"> TO <form name="checkage" onsubmit="saveSplash('.site.com'); return false;" method="post"> Quote Link to comment Share on other sites More sharing options...
mjurmann Posted June 26, 2008 Author Share Posted June 26, 2008 Please check your e-mail. Thank you. Quote Link to comment Share on other sites More sharing options...
mjurmann Posted June 26, 2008 Author Share Posted June 26, 2008 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> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted June 26, 2008 Share Posted June 26, 2008 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/';" ?> } Quote Link to comment Share on other sites More sharing options...
fanfavorite Posted June 26, 2008 Share Posted June 26, 2008 I didn't receive any email. My email on here goes to a box that gets a lot of spam, etc. I cannot seem to find your email, nor is there an pm on here. Aaron gave you another solution though that should work nicely for you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.