• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

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,


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!
case 'nz' : 	url = 'http://www.d-fa.com/store/';
case 'rest' : url = 'http://www.d-fa.com/store/';
window.location.href = url;


Open in new window

  • 2
1 Solution
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.
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.
willnjenAuthor Commented:
I've requested that this question be deleted for the following reason:

I don't need the answer now.
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now