![]() |
![]() |
|
|
||
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. JavaScript to discover that has been developed. See how it works at the demonstration page http://willmaster.com/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:
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 http://willmaster.com/a/14/pl.pl?art146 has instructions. By: Will Bontrager Copyright 2002 Bontrager Connection, LLC
| ||