PDA

View Full Version : Redirect By Cookie



TimFA
10-21-2007, 07:18 PM
I found this script:

External:


// Redirect Cookie 2
// Version 1.10
// Written by Chris Hansen.
// Copyright 1999-2003 by Chris Hansen.

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 "";
}

function SetCookie(name, value){
expDays = 365;
exp = new Date();
exp.setTime(exp.getTime() + (expDays * 24 * 60 * 60 * 1000));

document.cookie = name + "=" + escape(value) + "; expires=" + exp.toGMTString();
}

function cRedirect(value){
SetCookie("cChoise", value);
}

function IsCookieSet()
{
var redirpage = GetCookie("cChoise");
if (redirpage != "") {window.location = redirpage;}
}


Head:


function menuRedirect(){
var select = eval(document.redirectForm.elements[0].selectedIndex);
var choise1 = "choise1.html";
var choise2 = "default/index.html";

if (select==0) void(0);
else if (select==1) {window.open(choise1,"_top"); cRedirect(choise1);}
else if (select==2) {window.open(choise2,"_top"); cRedirect(choise2);}
}


Menu:


<FORM name="redirectForm">
<TABLE border="1" cellspacing="0" cellpadding="0">
<TR>
<TD>
<SELECT name="menu" onChange="menuRedirect()">
<OPTION value="1">Make A Choise</OPTION>
<OPTION value="2">Choise 1</OPTION>
<OPTION value="3">Choise 2</OPTION>
</SELECT>
</TD>
</TR>
</TABLE>
</FORM>


I've found 2 problems, 1 if I try to change anything, the cookie part (top) has to be removed otherwise nothing happens, I suspect theres an ID I'm missing and not changing, but thats minor. For some reason, whenever I run that in my browser it doesn't remember after a close. I looked it over and tried changing a few things, but no luck. :confused:

magicgavin
10-22-2007, 07:24 PM
This is what the code in the head should be. Notice the onload event is the only change


function menuRedirect(){
var select = eval(document.redirectForm.elements[0].selectedIndex);
var choise1 = "choise1.html";
var choise2 = "default/index.html";

if (select==0) void(0);
else if (select==1) {window.open(choise1,"_top"); cRedirect(choise1);}
else if (select==2) {window.open(choise2,"_top"); cRedirect(choise2);}
}
window.onload=IsCookieSet;