Link to home
Start Free TrialLog in
Avatar of willnjen
willnjen

asked on

Javascript cookie for whole domain

Hi Experts

I have two websites.  When a person visits the home page of the website1 they get the choice of staying on 1 or switching to website2.  I have a javascript redirect that drops a cookie and works OK.  If they return to the home page of website1 it remembers their choice and  auto-redirects them to website2.

How do I change my code so that it works across the whole domain rather than just the home page?  Eg if the first time they visit any page on website1 they are given the choice then the cookie remembers and if they return to website1 it will redirect them to their previous choice.

My code that works on a single page is below.  How can I edit it to work on a whole site?

Many thanks,

Will

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('dfa-country');

if (favorite != null) {
switch (favorite) {
case 'usa' : 	url = 'http://www.d-fa.com/usa-store/'; // change these!
	     	break;
case 'nz' : 	url = 'http://www.d-fa.com/store/';
	     	break;
case 'rest' : url = 'http://www.d-fa.com/store/';
		break;
}
window.location.href = url;

}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
(no points) just want to reiterate what Dave has said that you need to set the cookie at the root of the domain as it works downwards from there.  if you set the cookie in a subdirectory  then you will not be able to read the cookie in a directory above it.
Avatar of willnjen
willnjen

ASKER

I've requested that this question be deleted for the following reason:

I don't need the answer now.
You may not need it now but that doesn't mean the question hasn't been answered.
Dave's comment above certainly answers the question.