Click here to Skip to main content
13,094,732 members (96,822 online)
Click here to Skip to main content
Add your own
alternative version


50 bookmarked
Posted 11 Dec 2000

JavaScript For Beginners 2: Cookies

, 15 Mar 2001
Rate this:
Please Sign up or sign in to vote.
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 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, 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, 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 Believing this all is true I found and used examples for cookies using at

    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


    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;
    • 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
      • 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)


    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

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

    You can see source for browser identification examples at


    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


    About the Author

    Tibor Blazko
    Software Developer (Senior)
    Slovakia Slovakia
    No Biography provided

    You may also be interested in...

    Comments and Discussions

    QuestionHmmm... Pin
    Smallishfry24-Oct-05 20:18
    memberSmallishfry24-Oct-05 20:18 
    GeneralSome Good Resources Pin
    Anonymous19-Aug-05 17:31
    sussAnonymous19-Aug-05 17:31 
    GeneralGood work but it is not that easy. Pin
    Baldwin Martin16-Mar-01 5:51
    memberBaldwin Martin16-Mar-01 5:51 
    GeneralIts worse than you say Pin
    Shimon Crown10-Dec-00 22:27
    memberShimon Crown10-Dec-00 22:27 
    GeneralRe: Its worse than you say Pin
    Chris Maunder16-Mar-01 0:09
    memberChris Maunder16-Mar-01 0:09 
    GeneralRe: Its worse than you say Pin
    Anonymous26-Jul-03 22:47
    sussAnonymous26-Jul-03 22:47 
    GeneralRe: Its worse than you say Pin
    Faiz VP2-Mar-07 2:32
    memberFaiz 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.

    Permalink | Advertise | Privacy | Terms of Use | Mobile
    Web03 | 2.8.170813.1 | Last Updated 16 Mar 2001
    Article Copyright 2000 by Tibor Blazko
    Everything else Copyright © CodeProject, 1999-2017
    Layout: fixed | fluid