web blazonry web development scripts and tutorials
 
Web blazonry.com
   PHP       Name Generators       Perl       CSS       Javascript       Java       MySql       How Tos       Resources   

Javascript Home


image scripts
  Image Load Order
  Mouse Overs
  SlideShow

JavaScript Resources
  Free JavaScript Books(new)
  Recommended Sites
  Bestselling Books

Bookmark and Share





JavaScript: How to Work With Cookies (Part 3)

Retrieving a Cookie

The code to retrieve a cookie is a little more complicated, because you have to scroll through all cookies to find yours. Here is the JavaScript getCookie function:

<script language="JavaScript">

function getCookie (name) {
    var dc = document.cookie;
    var cname = name + "=";

    if (dc.length > 0) {
      begin = dc.indexOf(cname);
      if (begin != -1) {
        begin += cname.length;
        end = dc.indexOf(";" begin);
        if (end == -1) end = dc.length;
        return unescape(dc.substring(begin, end));
        }
      }
    return null;
}
</script>

Let’s retrieve the cookie we just set and display the value in a text box:

The HTML to retrieve the value of myCookie (the cookie we just set) is:

<form>
<input type="button" value="Get Cookie Value" onClick="this.form.tf.value = getCookie('myCookie')">
<input type="text" name="tf" size="30">
</form>

Special thanks to Brig at IntoText.com for pointing out and offering a fix for my typo in the getCookie function above.

Next >> Deleting a Cookie

 

Related Links:

    Cookie Central -- a site devoted completely to information about cookies.

 

Newest Pages
Test Google Ads Ver. 2
Free Linux Admin Books
Free Linux Books for Programmers
Free Books for Linux on the Desktop
Free PHP Books
Free JavaScript Books
Free Java Books - Advanced
Free Java Books - Basic
Free Perl Books
Free Python Books
Quote of the Day (PHP)
Debugging Part 2
How to Test Google Ads
Most Popular Pages
U.S. Name Generator
Baby Name Generator
Wu Name Generator
Popup Windows (JavaScript)
Intro to Web Databases (PHP, MySQL)
Upload and Resize an Image (PHP)
How To Install Apache + PHP + MySQL

Least Popular Pages
iNews Aplet (Java)
Java Resources)
Site Monitor (Perl)
Load Order of Images(Javascript)
 
 

  privacy policy     ||     © 1997-2010. astonishinc.com   All Rights Reserved.