JavaScript
  Home arrow JavaScript arrow Page 6 - Using Cookies With JavaScript
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Using Cookies With JavaScript
By: Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 147
    2004-04-26


    Table of Contents:
  • Using Cookies With JavaScript
  • Caveat Emptor
  • What's In A Name?
  • Cookie-Cutter Code
  • Running The Numbers
  • Speaking Native
  • Dinner Time

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Using Cookies With JavaScript - Speaking Native
    ( Page 6 of 7 )

    So that takes care of writing cookies, and then reading the values back. Now, how about a composite example that you can actually use?

    In this next example, I'll assume a multi-lingual Web site, where the user has a choice of viewing the content in either French or English. The first time the user visits the site, (s)he is permitted to select one of the two languages. This choice is stored in a cookie, so that on the next visit, the client is automatically redirected to the chosen language without requiring the user to re-select a language.


    <html>
    <head>
    <script language="JavaScript" src="cookieLibrary.js"></script>
    <script language="JavaScript">
    // check to see if cookie exists
    // if yes, redirect to language page
    if (GetCookie("lang") == "EN")
    {
     document.location.href="index.en.html";
    }
    else if (GetCookie("lang") == "FR")
    {
     document.location.href="index.fr.html";
    }
     
    // function to write selected language to cookie
    function lang(l)
    {
     // set expiry date for 1 year from now
     var d = new Date();
     d.setDate(d.getDate() + 365); 
     
     // write cookie
     SetCookie("lang", l, d);
     
     // take user to appropriate language page
     if (l == "EN")
     {
      document.location.href="index.en.html";
     }
     else if (l == "FR")
     {
      document.location.href="index.fr.html";
     }
     
    }
    </script>
     
    </head>
     
    <body>
     
    <h2>
    Please select your language:
    <ul>
    <li><a href="javascript:lang('EN');">English</a>
    <li><a href="javascript:lang('FR');">French</a>
    </ul>
    </h2>
     
    </body>
    </html>

    That was simple enough. When the user visits the page, the code first checks to see if a cookie with the selected language already exists. If it does, the browser is automatically redirected to pages in that language. If no cookie exists, it implies that the user has not selected a language, and so a language choice is displayed. The language selected is then written to a cookie in preparation for the next visit.



     
     
    >>> More JavaScript Articles          >>> More By Nariman K, (c) Melonfire
     

       

    JAVASCRIPT ARTICLES

    - Introduction to JavaScript
    - Adding Elements to a Tree with TreeView jQue...
    - Using the Persist Argument in a TreeView jQu...
    - Using Unique and Toggle in a TreeView jQuery...
    - Using Event Delegation for Mouseover Events ...
    - Using the Animate Option in a Treeview jQuer...
    - Using HTML Lists with Event Delegation in Ja...
    - Opened and Closed Branches on a TreeView jQu...
    - Mouseover Events and Event Delegation in Jav...
    - Creating a TreeView JQuery Hierarchical Navi...
    - Event Delegation in JavaScript
    - A Look at the New YUI Carousel Control
    - Working with Draggable Elements and Transpar...
    - Displaying Pinned Handles with Resizable Con...
    - Building Resizable Containers with the Ext J...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT