Javascript set cookie
What
is Cookie? Cookie is a piece of information browsers put on
the client's PC. Cookie include 6 part, which are name,value,expire
date, path, domain and security. A cookie always has its name
and value.(Otherwise, you don't need set up a cookie.) The
other 4 parts are optional.
Expire date is used to set cookie lifespan.
If a cookie (like document.cookie ="username=bill;" )
without expire date, this cookie will be gone when the user
closes his brower.
Path sets up restriction for sub-directories
accession. For example, if www.yourwebsit.com/bill/tools sets
a cookie, subdirectory www.yourwebsit.com/bill/checkout could
not read that cookie unless you set cookie like
document.cookie="username=Bill;path="/bill;";
This way, all subdirectories under /bill (eg,/tools and /checkout)
can share their cookies.
If set "path=/;", the
cookie will be available to all directories on the domain where
it is set from.If the web site is just one of that domain,
such as a free hosting usually have a lot of uers sharing same
directory, the other user also could access the cookie you
set.
Domain: By default, collies are only accessable
to pages in the domain they are set from. But if a site have
subdomains, like forum.yourwebsite.com and photos.yourwebsite.com,
a cookie set by a domain is not available to the other one.
In order to share cookie cross subdomain, cookies could set "domain=yourwebsite.com".
Security: By default, the value of security
is false.It means a cookie is always send back to the pages.
If the value is true, it requires a server with Secure sockets
layer(SSL).
In order to use the example in this page, your web browser
should accept cookies.
How
to do set cookies? Copy the following scripts to <head></head> section.
Click
here to see how to get cookie.
|