1. Computing & Technology

JavaScript By Example

Cookies: 4. Folder Specific Cookies

From , former About.com Guide

The opposite of making a cookie available to the entire domain is limiting it to a particular folder (or directory) of your site.We do this by specifying a path for our cookie. One thing to note when you limit where your cookie is accessible from like this is that browsers store a limited number of cookies per domain and restricting your cookie to a specific path will not change the maximum number of cookies you can have for your domain.

In this example we modify our writeCookie function to make the cookie it writes apply only topages in /myfolder.


writeCookie = function(cname, cvalue, days) {
var dt, expires;
dt = new Date();
dt.setTime(dt.getTime()+(days*24*60*60*1000));
expires = "; expires="+dt.toGMTString();
document.cookie = cname+"="+cvalue+expires+'; path=/myfolder';
}

©2012 About.com. All rights reserved.

A part of The New York Times Company.