CODE
<script LANGUAGE="JavaScript">
cookie_name = "dataCookie";
function putCookie() {
if(document.cookie != document.cookie) <-- This is a weird piece! (*)
{index = document.cookie.indexOf(cookie_name);}
else
{ index = -1;}
</SCRIPT>
<script LANGUAGE="JavaScript">
cookie_name = "dataCookie";
function getName() {
if(document.cookie)
{
index = document.cookie.indexOf(cookie_name);
if (index == -1)
{java script:go ('home.htm');}
}
</SCRIPT>
(*): document.cookie will always be the same as document.cookie, so the following piece of code will never be executed.
Now, what you do is looking if the cookie contains 'dataCookie', if it doesn't, it will go to 'home.htm' (I think the function go() doesn't excist, I always use 'window.location = something'.)
If I see your code, I think there won't ever be a cookie. First: the code won't be executed (see (*)) and if it would be executed, it would be a session-only cookie. To be honest, if I were you, I would make this script all over again. (But I'll do it for you now.)
CODE
<script language='JavaScript'>
function setCookie(page){ //This is the function that sets the cookies
var date = new Date(); //A date-var
var expires = 24*60*60*1000; //This is the time the cookie is valid in milliseconds (This is 1 day)
date.setTime(date.getTime()+expires); //This is where we see when the cookie is no longer valid
var expires = "; expires="+date.toGMTString(); //We're going to need this for the cookie
document.cookie = "page=" + page + expires; //Filling the cookie with data (Important: this cookie needs the variable expires!
window.location = page; //Redirecting to the given page
}
function getCookie(){ //This function will check the cookies
if(!document.cookie) return; //If no cookie: return
var cookie = document.cookie; //Getting the data from the cookie to a var
var cookie = cookie.split("="); //Splitting the data at '='
j=false; //This will be clear a few lines later
for(i=0;i<cookie.length;i++){ //A for-loop that will run once for each piece of the array cookie
if(cookie[i] == "page") j=i+1; //If the piece of the array is equal to "page" our var in the first function, we save the position + 1 in the var j. Why 'i+1': this is the piece of the array that will contain the page, since we splitted at '='
}
if(!j) return; //If j is not set, see the line 'j=false', return. (This is when there is a cookie, but it doesn't contain the right data.
document.location = cookie[j]; //And again: redirect to the given page
}
window.onload = getCookie; //Makes the function getCookie() start when the page is loaded
</SCRIPT>
This script will check for cookies when you open the page, if the cookie is set, it will redirect to the given page.
How to use it: in you page should be links, buttons, ... to the following url: 'java script:setCookie(page)'. (don't type 'page', but this should be the url you want to redirect to. Example: 'home_en.html'.)
Hope this was usefull for you.
Reply