Discover If a Browser Accepts Cookies
When you depend on cookies for certain site functions, it would be nice to know if the visitor's browser is actually accepting them.
Let's suppose you have a popup on your site, one supposed to pop up only once for each individual site visitor. When the popup launches the first time, it sets a cookie indicating the fact. Thereafter, the JavaScript recognizes the cookie and won't launch another popup for that browser.
When the browser won't accept cookies, the popup will launch the first time, then again and again and again. The cookie never gets set. Every time the JavaScript doesn't find the cookie, it launches the popup.
This can be very annoying to those who have cookies turned off.
If you have an e-commerce site with a shopping cart, the shopping cart may require cookies. For folks with cookies turned off, it can be annoying when items won't accumulate in the cart.
Those are just two examples. Sites that must (or want to) use cookies, can benefit by discovering whether or not the browser accepts them.
Cookies are the easiest and most reliable method of keeping track of which functions a browser has participated in (such as launching a popup window) and to store values for future page views (such as shopping cart contents). Other methods can be utilized, such as IP address/referrer tracking, but these are more complicated to implement and are less reliable than using cookies.
The solution is to discover if the browser accepts cookies.
This JavaScript to discover if cookies are accepted has been developed. See how it works at the demonstration page /a/14/pl.pl?art146 If your browser accepts cookies, you'll get an alert box saying so. Conversely, if your browser doesn't accept cookies, the alert box will say that.
If you use this code to discover if the browser will accept cookies before launching a popup window, for example, you would replace the code that generates the "yes" alert box with the function that generates your popup.
If your shopping cart requires cookies, you would replace the "no" alert box text to inform the user that cookies must be turned on.
Instructions are on the demo page.
The JavaScript is only 25 lines. A link on the demonstration page lets you download a working page, which also has the instructions.
You simply replace the example code that launches the appropriate alert box with the name of the JavaScript function that you want to run.
This is how it works:
-
As your page loads, the JavaScript will attempt to set a cookie.
(This cookie, if successfully set, will automatically delete when the browser is closed.)
-
Three seconds after that, the JavaScript will attempt to read the cookie.
(Three seconds was chosen to ensure that any browser will have time to set the cookie and record the fact in its memory. That's probably much more time than any browser needs, but I was playing safe. Probably one second would be plenty, or even less. If you want to change to a different wait period, change the fifth line of the JavaScript:
setTimeout('TestIfCookieWasSet()',3000);
The number 3000 means wait 3000 milliseconds. You can change that number as you wish.)
-
If the cookie is successfully read, it means the browser allowed the test cookie to be set. An appropriate alert message will display (or other action if you customize).
-
If the cookie can not be read, the test cookie was not set. An appropriate alert message will display (or other action if you customize).
Simply put, the JavaScript tests whether or not the browser will accept a cookie. You determine what to do if it does and/or if it does not.
If you don't want the discovery test to be done while the page is loading, you can delay. The demonstration page at /a/14/pl.pl?art146 has instructions.
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]
Will Bontrager
©2002 Bontrager Connection, LLC
Please note:
Articles on this website are presented "as is". However -
If you have a question about a CGI script, HTML, CSS, PHP, or JavaScript
Ask one of our Experts and you'll have your answer!
Click here for details.