Back
Next
What is a JavaScript cookie?
JavaScript
Cookies writing is a technique that enables a web site
owner to store information about a given user for submission in a form, or so
manipulation at a later browsing session by the same use. Being that JavaScript cookies are
stored on the user's machine mean on client-side, they do not present a fail-safe way to
handle information, but they are essential in accomplishing this task in situations
where the web programmer does not have access so server-side programming and scripting
plate forms such as ASP, JSP and PHP.
A JavaScript cookie is a piece of data stored by a web browser on a user's hard disk.
JavaScript cookie storage is rigidly controlled for security purposes, and the amount of data that can be stored which varies between browsers is small - typically
4K bytes per JavaScript cookie.
Hiding JavaScript cookies
JavaScript cookies are useful, but they're also unreliable. Some users set their
web browsers not to accept JavaScript cookies,
and cookies can 'disappear' from a disk for a variety of reasons, including space limitations or the
user switching to a different web browser. JavaScript cookies should never be used to store irreplaceable data, and
JavaScript cookie-handling code should always be defensive, i.e. able to handle situations where an expected
JavaScript cookie
isn't found.
Cookies Handling in JavaScript
Using JavaScript, we can create and use cookies direct from web pages. This is
called 'client-side' cookie handling because the code runs in the browser, or
'client', as opposed to server-side JavaScript cookie handling, where code running on the
web server sends cookie-handling instructions to the browser. The code here will runs on Microsoft and Netscape browsers from
version 3.0 onwards, as well as other compatible browsers such as Opera.
Properties of JavaScript Cookies
JavaScript Cookie Property |
Description of JavaScript Cookie |
Example of JavaScript Cookie |
name =value |
This sets both the JavaScript cookie's name and its value. |
username=scott |
expires=date |
This optional value sets the date that the JavaScript cookie will expire on.
The date should be in the format returned by the toGMTString()
method of the Date object. If the expires
value is not given, the JavaScript cookie will be destroyed the moment the browser
is closed. |
expires=
13/06/2003 00:00:00 |
path=path |
This optional value specifies a path within the site to which the
cookie applies. Only documents in this path will be able to retrieve the
JavaScript
cookie. Usually this is left blank, meaning that only the path that set
the JavaScript cookie can retrieve it. |
path=/JavaScript
_Tutorial/ |
domain=domain |
This optional value specifies a domain within which the JavaScript cookie
applies. Only websites in this domain will be able to retrieve the
cookie. Usually this is left blank, meaning that only the domain that
set the cookie can retrieve it. |
domain=
globalguideline.com |
secure |
This optional flag indicates that the browser should use SSL when
sending the JavaScript cookie to the server. This flag is rarely used. |
secure |
How to create and retrieve JavaScript cookies
Back
Next
|