Some site features must have cookies to work correctly.
Most shopping carts, for example, use cookies to maintain a list of what
the customer has placed into the cart. Some CGI games require cookies, and
some multi-page forms require cookies.
Some web sites, although they work without cookies, can provide a better
visitor experience with them.
Many browsers have cookies enabled. Some don't. And some have only certain
types of cookies enabled, like session cookies.
This article shows how to use JavaScript to test the visitor's browser for
session cookies the type of cookie that automatically expires when
the browser is closed. The JavaScript can be placed on a different web site
to test third-party cookies. Modification of the JavaScript can test
persistent cookies.
A Browser Cookie Test
To test whether or not the visitor's browser is cookie-enabled, put this
JavaScript somewhere into your web page. It can be in the HEAD area or the
BODY.
The above redirects the browser to http://example.com/store.html if the
browser accepted the test cookie. Otherwise, an alert message is displayed.
If you do not wish to redirect or cause other actions when cookies are
enabled, only to display an alert message when the cookie doesn't exist,
then remove the line in the JavaScript that begins withelse { window
Theelse { window ... code
may be replaced with other actions, presenting a coupon for a special item
in the store, for example.
Types of Cookies
The types of cookies relate to how long they last and who sets them.
Session Cookies
Cookie that automatically expire when the browser is closed are called session cookies. They exist only during the current browser session.
Persistent Cookies
Persistent cookies are those that will live for 1 or more days. The number of days is specified when the cookie is set.
This JavaScript will set a persistent cookie to test whether or not a browser will accept it. (Some browsers are configured to accept session cookies, but not peristent or third-party cookies.)
The above JavaScript sets the test cookie for lasting 30 days, which can be changed at the third line of the code.
Third-Party Cookies
Third-party cookies are cookies set by a domain other than the one in the browser's address bar.
Third-party cookies can be set when an element on the page (an image or the contents of an IFRAME, for examples) is provided by a different domain. That other domain sets the cookie at the time it sends the browser what it asked for.
To test whether or not a browser will accept third-party cookies, put a web page containing the JavaScript from one of the above textareas on a different domain. Then, make a web page for your regular domain that contains an IFRAME that loads the web page on that other domain. Here is an example IFRAME:
Change the SRC to the URL of the web page on the other domain.
Now, whenever the web page with the IFRAME is loaded into a browser, a third-party cookie test will be done.
Question:
Did you find this article interesting and understandable? How can it be improved?
Your response is anonymous.
When done typing, click anywhere outside the box. [more info]