Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

JavaScript For Beginners 2: Cookies

0.00/5 (No votes)
15 Mar 2001 1  
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