Click here to Skip to main content
15,891,375 members
Articles / Web Development / HTML
Article

JavaScript For Beginners 2: Cookies

Rate me:
Please Sign up or sign in to vote.
2.83/5 (5 votes)
15 Mar 2001 119K   1.4K   50   7
An introduction to using Cookies in Javascript
  • Download example scripts - 1 Kb
  • Introduction

    This article follows the great JavaScript for Beginners article and hopes to bring its readers to the cruel reality of life by discussing cookies .

    The problem I wanted to solve was "automated" access to http://www.thehungersite.com. This page has the ability to limit you to one visit per day only (don't forget to click on the link above!). Until now, each time I first started my browser I would manually choose a bookmark to load the page.

    Why not create simple script for it?

    Because I wanted to have it work under Netscape as well as IE I started to study JavaScript.

    The Solution

    Main idea is simple: create a page what will determine whether it has already been loaded today, and if not make it switch to http://www.thehungersite.com, and set this page as browser's home page.

    Getting the actual page and redirecting is easy. The problem was remembering.

    Because JavaScript has no file access functions (and looking into examples like http://www.javascript.sk/skript.php3?150, no vector graphics either) we must use so called cookies. (also see MSDN - Platform SDK/Tools and Languages/Scripting/JScript/FileSystemObject User's Guide/Working with Files. There are examples for file access from JScript using ActiveX objects, but of course this is only good for Microsoft browsers).

    Cookies are size-limited variables associated with a server's domain. By default cookie expire when the browser closes (not when you leave the page!) but a script programmer can change this. Persistent cookies are stored at the users end, separately for every browser user and separately for every browser (Netscape uses for cookies in file, Internet Explorer stores every cookie in a separate one). This difference in browsers brings surprises at some servers where you must log in again and again Be aware that a user can disable cookies in his or her browser settings.

    For a detailed cookie description you can see http://www.netscape.com/newsref/std/cookie_spec.html. Believing this all is true I found and used examples for cookies using at http://www.javascript.sk.

    All was fine and nice until time I not tried it with Internet Explorer. Then after I called some example Javascript:

    cookieExpires = "01-APR-" + nLyear + " GMT";
    document.cookie = cookieName + "=" + cookieValue + "; expires=" + cookieExpires;
    

    and then called

    document.write(document.cookie);
    

    document.cookie
    was empty.

    After experiments and searches in examples I found that

    • you cannot read/display the cookie expriry part. If you want to know it you must assign the same to another simple string variable:
      document.cookie = cookieName + "=" + cookieValue + "; expires=" + cookieExpires;
      myvar = cookieName + "=" + cookieValue + "; expires=" + cookieExpires;
      document.write(myvar);
      
    • browsers use different date format:
      • Netscape ends with "GMT", IExplorer "UTC", because of this it is good to use constructions like
        var expdate = new Date()
        cookieExpires.setTime (expdate.getTime() + 1 * (24 * 60 * 60 * 1000)) //+1 day
        cookieExpires.toGMTString()
        
      • When you display Date parts
        document.write(expdate.getYear() + "<br>" + expdate.getMonth() + "<br>" + expdate.getDate());
        
        for 15. November 2000 you will see 2000/10/15 under Internet Explorer, and 100/10/15 under Netscape
        (Editors Note: Be aware of the Y2K problem with some Netscape browsers)
        .
      • In examples I saw parts like
        if (platform == "Mac") {
          lastVisit = lastVisit - (24 * 60 * 60 * 1000)
        }
        
        but I have no possibility to check it.
    • Date has getDate and getDay methods, second returns index of day in week

    Knowing this, the rest was without problems: (See attached file: homepage.htm)

    Conclusion

    There is not a single JavaScript only.

    If you want use it in your web pages you must minimally test it with different browsers - and their versions. Many scripts contain browser type and version checking and lots of if-the-else's to handle the differences..

    Short vocabulary for http://www.javascript.sk

    ukazka demonstration
    zdrojovy kod source code
    prirucka manual
    priklad(y) example(s)

    You can see source for browser identification examples at http://www.javascript.sk/prirucka_sk/cast5.html.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    Software Developer (Senior)
    Slovakia Slovakia
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    QuestionHmmm... Pin
    Smallishfry24-Oct-05 20:18
    Smallishfry24-Oct-05 20:18 
    GeneralSome Good Resources Pin
    Anonymous19-Aug-05 17:31
    Anonymous19-Aug-05 17:31 
    GeneralGood work but it is not that easy. Pin
    Baldwin Martin16-Mar-01 5:51
    Baldwin Martin16-Mar-01 5:51 
    GeneralIts worse than you say Pin
    10-Dec-00 22:27
    suss10-Dec-00 22:27 
    A nice and useful article.

    Unfortunately it is not only a matter of testing between different makes of browser - e.g. IE vs Netscape but you must test different versions of the same browser. I have a perfectly legal script that works on IE5.5 but doesn't on IE5.0. With Netscape things are even worse as Netscape 4.x is not related at all to Netscape 6.


    GeneralRe: Its worse than you say Pin
    Chris Maunder16-Mar-01 0:09
    cofounderChris Maunder16-Mar-01 0:09 
    GeneralRe: Its worse than you say Pin
    Anonymous26-Jul-03 22:47
    Anonymous26-Jul-03 22:47 
    GeneralRe: Its worse than you say Pin
    Faiz VP2-Mar-07 2:32
    Faiz VP2-Mar-07 2:32 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.