JavaScript
  Home arrow JavaScript arrow Page 6 - Using Cookies With JavaScript
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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: 4 stars4 stars4 stars4 stars4 stars / 132
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    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

    - Getting Attention with Interactive Effects
    - Interacting with Tooltips and Previews
    - Just-in-Time Information and Ajax
    - Interactive Effects
    - Using Cookies With JavaScript
    - Understanding the JavaScript RegExp Object
    - Controlling Browser Properties with JavaScri...
    - Using Timers in JavaScript
    - Form Validation with JavaScript
    - JavaScript Exception Handling
    - Stringing Things Along
    - Understanding The JavaScript Event Model (pa...
    - Understanding The JavaScript Event Model (pa...
    - An Object Lesson In JavaScript




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway