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

willnjenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
On this page http://en.wikipedia.org/wiki/HTTP_cookie under "Expires and Max-Age" are some good examples.  Basically you set the "Path=/" and "Domain=.example.com".  Note the dot '.' in front of the domain name.  This is used to tell the browser that all subdomains are also included.  "Path=/" means from the root of the domain and all paths under that.

To make it work on any and all pages on website1, you must have the redirect code on each and every page of website1.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RobOwner (Aidellio)Commented:
(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.
0
willnjenAuthor Commented:
I've requested that this question be deleted for the following reason:

I don't need the answer now.
0
RobOwner (Aidellio)Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JScript

From novice to tech pro — start learning today.