You are here: irt.org | Articles | JavaScript | Cookie | Intelligent Cookies [ previous next ]
Published on: Saturday 21st February 1998 By: Martin Webb
Have you ever come across a web site, that refuses to accept the fact that you don't take cookies? This article will describe how to utilise cookies in your web site without forcing cookies on your visitors.
We can then use cookies, if the visitor permits, to track the visitor through the site, without the danger of upsetting the visitor.
Rightly or wrongly people are wary of cookies, and as a result they sometimes set the options on their browser to warn them if a site attempts to write a cookie. The user can then decide whether to allow the site to write the cookie or not. However badly written scripts could still attempt to write the same cookie or other further cookies without realising that the visitor doesn't want them. If this goes on for too long then the visitor may leave - disaster!
The browser can warn if a cookie is being written, however it remains silent if the cookie is being read. It is this facility that will be used to create Intelligent Cookies.
Normally when writing a cookie - all that is done is the writing of the cookie. If we were to check to see if the cookie had actually been written then we would know whether the visitor is accepting cookies. Before writing the next cookie, we should check that the last cookie was written successfully. If it wasn't then we shouldn't write the next one.
Rather than use JavaScript variables to work out whether the last cookie was written or not, we'll use a Master Cookie itself as an indication of whether or not any further cookies should be written.
If the visitor accepts the first few cookies, but then stops accepting them, for whatever reason, then we should realise this and stop the writing of any further cookies. In this instance we need to delete the Master Cookie. This will then indicate that no further cookies are acceptable. Note: it is possible that the visitor could deny this as well.
The following psuedo code explains the mechanism:
store Master Cookie get Master Cookie if Master Cookie does not exist set Master Cookie store Intelligent Cookie get Master Cookie if Master Cookie get Intelligent Cookie if Intelligent Cookie does not exist or its value is different set Intelligent Cookie get Intelligent Cookie if Intelligent Cookie does not exist or its value is different delete Master Cookie
The store Master Cookie code only needs to be invoked from the sites Home Page, i.e. when the visitor arrives. The store Intelligent Cookie code can be invoked any time an Intelligent Cookie needs to be stored.
The Master Cookie will be stored without an expiry date, which means that once the visitors current browser session has finished the cookie will disappear. Next time the visitor comes by the Master Cookie will be stored again. If the visitor refuses to accept cookies, then the worse they'll receive is a request to store the Master Cookie each time they visit the Home Page.
The code Get_Cookie(), Set_Cookie() and Delete_Cookie() is based on the public domain cookie code produced by Bill Dortch.
function Get_Cookie(name) { var start = document.cookie.indexOf(name+"="); var len = start+name.length+1; if ((!start) && (name != document.cookie.substring(0,name.length))) return null; if (start == -1) return null; var end = document.cookie.indexOf(";",len); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(len,end)); } function Set_Cookie(name,value,expires,path,domain,secure) { document.cookie = name + "=" +escape(value) + ( (expires) ? ";expires=" + expires.toGMTString() : "") + ( (path) ? ";path=" + path : "") + ( (domain) ? ";domain=" + domain : "") + ( (secure) ? ";secure" : ""); } function Delete_Cookie(name,path,domain) { if (Get_Cookie(name)) document.cookie = name + "=" + ( (path) ? ";path=" + path : "") + ( (domain) ? ";domain=" + domain : "") + ";expires=Thu, 01-Jan-1970 00:00:01 GMT"; }
var today = new Date(); var zero_date = new Date(0,0,0); today.setTime(today.getTime() - zero_date.getTime()); var todays_date = new Date(today.getYear(),today.getMonth(),today.getDate(),0,0,0); var expires_date = new Date(todays_date.getTime() + (8 * 7 * 86400000)); function storeMasterCookie() { if (!Get_Cookie('MasterCookie')) Set_Cookie('MasterCookie','MasterCookie'); } function storeIntelligentCookie(name,value) { if (Get_Cookie('MasterCookie')) { var IntelligentCookie = Get_Cookie(name); if ((!IntelligentCookie) || (IntelligentCookie != value)) { Set_Cookie(name,value,expires_date); var IntelligentCookie = Get_Cookie(name); if ((!IntelligentCookie) || (IntelligentCookie != value)) Delete_Cookie('MasterCookie'); } } }
The storeMasterCookie() and storeIntelligentCookie() functions follow the psuedo code described earlier. They both make use of the Get_Cookie(), Set_Cookie() and Delete_Cookie() functions. The intial date manipulation is required to create an expiry date (set for 8 weeks into the future) for the storing of the Intelligent Cookies.
The writing and testing of these Intelligent Cookies can occur across many documents in a site. Rather than include the JavaScript code in each and every document, we'll utilise JavaScript source files to hold the JavaScript in a cookie.js file. These *.js files are supported in Netscape Navigator 3 upwards and in Microsoft Internet Explorer from around 3.02 upwards. For browsers that don't support *.js files then cookies will simply not be written. If you cannot live with this then you'll ned to include the code in all of your pages that write cookies.
The contents of the cookie.js file would be as follows:
function Get_Cookie(name) { var start = document.cookie.indexOf(name+"="); var len = start+name.length+1; if ((!start) && (name != document.cookie.substring(0,name.length))) return null; if (start == -1) return null; var end = document.cookie.indexOf(";",len); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(len,end)); } function Set_Cookie(name,value,expires,path,domain,secure) { document.cookie = name + "=" +escape(value) + ( (expires) ? ";expires=" + expires.toGMTString() : "") + ( (path) ? ";path=" + path : "") + ( (domain) ? ";domain=" + domain : "") + ( (secure) ? ";secure" : ""); } function Delete_Cookie(name,path,domain) { if (Get_Cookie(name)) document.cookie = name + "=" + ( (path) ? ";path=" + path : "") + ( (domain) ? ";domain=" + domain : "") + ";expires=Thu, 01-Jan-70 00:00:01 GMT"; } var today = new Date(); var zero_date = new Date(0,0,0); today.setTime(today.getTime() - zero_date.getTime()); var todays_date = new Date(today.getYear(),today.getMonth(),today.getDate(),0,0,0); var expires_date = new Date(todays_date.getTime() + (8 * 7 * 86400000)); function storeMasterCookie() { if (!Get_Cookie('MasterCookie')) Set_Cookie('MasterCookie','MasterCookie'); } function storeIntelligentCookie(name,value) { if (Get_Cookie('MasterCookie')) { var IntelligentCookie = Get_Cookie(name); if ((!IntelligentCookie) || (IntelligentCookie != value)) { Set_Cookie(name,value,expires_date); var IntelligentCookie = Get_Cookie(name); if ((!IntelligentCookie) || (IntelligentCookie != value)) Delete_Cookie('MasterCookie'); } } } var src_loaded = true;
To embed the cookie.js file into a page use the following:
<SCRIPT SRC="cookie.js"><!-- var src_loaded = false; //--></SCRIPT>
To later create the Master Cookie:
<SCRIPT LANGUAGE="JavaScript"><!-- if (src_loaded) storeMasterCookie(); //--></SCRIPT>
To store an Intelligent Cookie:
<SCRIPT LANGUAGE="JavaScript"><!-- if (src_loaded) storeIntelligentCookie('test','cookie value'); //--></SCRIPT>
The src_loaded variable indicates whether the cookie.js file was loaded by the browser or not.
Chocolate Chip Cookies + Automating NEW!