[thelist] Getting the base domain in javascript

Samuel Murphy smurphy at texterity.com
Tue Feb 3 13:50:27 CST 2004

I want to set the base (2nd) level domain when I
set a cookie so that any of the third level
domains can check the cookie. But I didn't want
to hard code a two level domain.

This is the javascript I came up with:

function baseDomainString(){
  e = document.domain.split(/\./);
  if(e.length > 1) {
    return("domain=" + e[e.length-2] + "." +  e[e.length-1]) + ";"  ;

document.cookie = "GlobalFlag=t;path=/;" + baseDomainString() + "expires=" +

Is there a more reliable way to get the base domain? Am I
missing the concept totally?



More information about the thelist mailing list