web blazonry web development scripts and tutorials
 
Web blazonry.com
   Home       PHP       Name Generators       Perl       CSS       Javascript       Java       devTips       Resources   

Javascript Home


image scripts
  Image Load Order
  Mouse Overs
  SlideShow

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





JavaScript: How to Work With Cookies

Cookies: Putting It All Together & Summary (Part 5)

Our cookie tutorial used JavaScript and HTML forms to create and work with a cookie file. In actual use, you would call your cookie functions when your page is loaded.

Put the JavaScript cookie functions at the top of your file like in the previous pages of this tutorial. After your HTML BODY tag, call the relevant cookie function.

Saving a Cookie
Save your cookie with code that looks like this:

<script language="JavaScript"><!--
var exp = new Date();     //create new date object
exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 30));     //set it 30 days ahead

setCookie("myCookie","it is crunchy", exp);     //save the cookie

//-->
</script>

Retrieving the Cookie
Retrieve the cookie with code like this:

<script language="JavaScript"><!--
getCookie("myCookie");
//-->
</script>

For debugging, you can print out the cookie to your browser:

<script language="JavaScript"><!--
document.write(getCookie("myCookie"));
//-->
</script>

Deleting the Cookie
Delete the cookie with code like this:

<script language="JavaScript"><!--
delCookie("myCookie");
//-->
</script>

Summary of Using JavaScript to Save, Retrieve and Delete a Cookie

There you have it. This wasn’t the most spectacular example of using a cookie, but this is meant to get you started. Understand the basics of working with cookies and then you can build from here and use cookies in more sophisticated and innovative ways. Have fun with it and hope it helps.


Return to First Page of Cookie Tutorial

 

Related Links:

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

 

Newest Pages
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
Source Code for Test Google Ads (PHP)
Most Popular Pages
U.S. Name Generator
Wu Name Generator
Baby Name Generator
Upload and Resize an Image (PHP)
Popup Windows (JavaScript)
How To Install Apache + PHP + MySQL
Simple Web Calendar (PHP)

Least Popular Pages
Image Load-Order (Javascript)
dHTML: Scrolling Layers
iNews Aplet (Java)
ProcLog Search Your Logs (Perl)
 
 

  © 1997-2008. astonishinc.com   All Rights Reserved.