Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1368 Can I access a cookie that is created in a different page than the current page?

You are here: irt.org | FAQ | JavaScript | Cookie | Q1368 [ previous next ]

Yes. If its in a different directory then you may need to set the path property of the cookie so that it is high enough in the directory tree not to prevent a document in one part of the directory tree from being excluded. For example, if you set the path to:

/topdir/dir1

then files in /topdir/dir2 will not be able to access the cookie. But if you set the path to:

/topdir

Then files in all sub-directories of topdir will have access. By default the path is set to:

/

Which means all files on the server have access to the cookie.

You set the path of the cookie by passing a path value to a function similar to the one below:

function Set_Cookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") +
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}

©2018 Martin Webb