Java Reference
In-Depth Information
If you want to specify that the cookie is available to all subdirectories of the domain it is set in, you can
specify a path of the root directory using the / character.
document.cookie = “UserName=Paul;expires=Tue, 28 Dec 2020 00:00:00;path=/;”;
Now, the cookie will be available to all directories on the domain it is set from. If the web site is just one
of many at that domain, it's best not to do this because everyone else will also have access to your
cookie information.
It's important to note that although Windows computers don't have case-sensitive directory names,
many other operating systems do. For example, if your web site is on a Unix- or Linux-based server, the
path property will be case-sensitive.
domain
The fourth part of the cookie string is the domain. An example of a domain is wrox.com or pawilton.
com. Like the path part of the cookie string, the domain part is optional and it's unlikely that you'll fi nd
yourself using it very often.
By default, cookies are available only to pages in the domain they were set in. For example, if you have
your fi rst web site running on a server with the domain MyPersonalWebSite.MyDomain.Com and you
have a second web site running under MyBusinessWebSite.MyDomain.Com, a cookie set in one web
site will not be available to pages accessed under the other domain name, and vice versa. Most of the
time, this is exactly what you want, but if it is not, you can use the domain part of the cookie string to
specify that a cookie is available to all subdomains of the specifi ed domain. For example, the following
sets a cookie that can be shared across both subdomains:
document.cookie = “UserName=Paul;expires=Tue, 28 Dec 2020 00:00:00;path=/” +
“;domain=MyDomain.Com;”;
Note that the domain must be the same: You can't share www.SomeoneElsesDomain.com with
www.MyDomain.com.
secure
The fi nal part of the cookie string is the secure part. This is simply a Boolean value; if it's set to true
the cookie will be sent only to a web server that tries to retrieve it using a secure channel. The default
value, which is false, means the cookie will always be sent, regardless of the security. This is only
applicable where you have set up a server with SSL (Secure Sockets Layer).
Creating a Cookie
To make life easier for yourself, you'll write a function that enables you to create a new cookie and set
certain of its attributes with more ease. This is the fi rst of a number of useful functions you'll create and
add to a separate .js fi le so you can easily re-use the code in your future projects. You'll look at the code
fi rst and create an example using it shortly. First create a fi le called CookieFunctions.js and add the
following to it:
function setCookie(cookieName, cookieValue, cookiePath, cookieExpires)
{
Search WWH ::




Custom Search