Page 1 of 1

Redirect cookies

Posted: Tue Aug 23, 2005 11:20 am
by londonphp
I am trying to incorporate this code into my site
http://javascript.internet.com/cookies/ ... irect.html

The code works fine, but i am trying to modify it as follows:

I have an intro page which will forward then to a page called HOME, this is where i will place the redirect script, I would like subsequent pages to then include an onLoad function (i think) in the <BODY> tag on the home page of each location. I have a 'cat' and a 'dog' for instance so rather than the user clicking on the cat or dog box i'd rather automatically place a cookie on the home page where they choose to navigate.


I have tried the onLoad function in the body tag, but using the above code it comes back with 'animalundefined'
where as it should come back with 'animalcat' were i to navigate to the cat home page.

not sure if this is clear to anyone?

(I am using this as a redirect for particular geographical locations, so for example once a viewer has for example looked at the london home page i want them to next time be automatically forwarded to this page as opposed to the HOME page, and i need this to work for a number of location, eg cardiff, Edinburgh.)

Posted: Wed Aug 24, 2005 3:57 am
by Grim...
Got any code? Or a link to your page?

Posted: Wed Aug 24, 2005 7:41 am
by londonphp
Heres the cookie code

Code: Select all

<!-- TWO STEPS TO INSTALL COOKIE REDIRECT:

  1.  Copy the coding into the HEAD of your HTML document
  2.  Add the last code into the BODY of your HTML document  -->

<!-- STEP ONE: Paste this code into the HEAD of your HTML document  -->

<HEAD>

<SCRIPT LANGUAGE="JavaScript">
<!-- Original:  Ronnie T. Moore -->
<!-- Web Site:  The JavaScript Source -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
var expDays = 30;
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function getCookieVal (offset) {  
var endstr = document.cookie.indexOf (";", offset);  
if (endstr == -1)    
endstr = document.cookie.length;  
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {  
var arg = name + "=";  
var alen = arg.length;  
var clen = document.cookie.length;  
var i = 0;  
while (i < clen) {    
var j = i + alen;    
if (document.cookie.substring(i, j) == arg)      
return getCookieVal (j);    
i = document.cookie.indexOf(" ", i) + 1;    
if (i == 0) break;   
}  
return null;
}
function SetCookie (name, value) {  
var argv = SetCookie.arguments;  
var argc = SetCookie.arguments.length;  
var expires = (argc > 2) ? argv[2] : null;  
var path = (argc > 3) ? argv[3] : null;  
var domain = (argc > 4) ? argv[4] : null;  
var secure = (argc > 5) ? argv[5] : false;  
document.cookie = name + "=" + escape (value) + 
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
((path == null) ? "" : ("; path=" + path)) +  
((domain == null) ? "" : ("; domain=" + domain)) +    
((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {  
var exp = new Date();  
exp.setTime (exp.getTime() - 1);  
var cval = GetCookie (name);  
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

var favorite = GetCookie('animal');

if (favorite != null) {
switch (favorite) {
case 'cat' : 	url = 'cat.html'; // change these!
	     	break;
case 'dog' : 	url = 'dog.html'; 
	     	break;
case 'gerbil' : url = 'gerbil.html';
		break;
case 'gopher' : url = 'gopher.html';
		break;
}
window.location.href = url;
}
//  End -->
</script>
</HEAD>

<!-- STEP TWO: Copy this code into the BODY of your HTML document  -->

<BODY>

<center>
<form>
<table><tr><td>
Please choose your Favorite Pet:<br>
<input type=checkbox name="cat" onClick="SetCookie('animal', this.name, exp);">Cat<br>
<input type=checkbox name="dog" onClick="SetCookie('animal', this.name, exp);">Dog<br>
<input type=checkbox name="gerbil" onClick="SetCookie('animal', this.name, exp);">Gerbil<br>
<input type=checkbox name="gopher" onClick="SetCookie('animal', this.name, exp);">Gopher<br>
</td></tr>
</table>
</form>
</center>

<p><center>
<font face="arial, helvetica" size="-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>

<!-- Script Size:  2.84 KB -->
I just need rather than the four checkboxes to have an onLoad function on the pages cat,.htm dog.htm,.. so once the visitor has visited one of these it will then basically we set as thier home page.

So for instance initially they come into index.php where they will then navigate the site, once they hit for example cat.htm an onLoad cookie will then set the cookie, thus next time they hit the site through index.php they will be redirected to cat.htm