• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Chapter 13. Remember with Cookies > Getting Information and Giving It Back

Getting Information and Giving It Back

The trick in using cookies, of course, is to have the user provide information and be able to get that information for use in the web page. Whether the information is for a simple greeting whenever the viewer opens her page or is used to configure the page being presented to the user, existing information must be placed in the cookie. This next script is a relatively simple one that asks the viewer for information that is stored in a cookie upon the press of a button. Then, by pressing a second button, the viewer is presented with only the value stored in the name attribute, but not the name itself or the date expiration information. Therefore, pay close attention to the formatting used for configuring input and output. (Remember that &nbsp is simply an HTML nonbreaking space.)

<title>Read and Write Cookie</title> 
<style type="text/css"> 
      body {
      font-family:verdana;font-size:11pt } 
      #display {color:rgb(169,33,53); background-color:rgb(249,225,203) } 
<script language="Javascript"> 
function yourCookie(name) {
      var remain=new Date("November 9, 2005"); 
      document.cookie= escape(name + document.baker.myCookie.value + "; expires=" + 
      remain.toGMTString( ) + ";"); 
function welcomeBack( ) {
      var seeMe= unescape(document.cookie); 
      var tagIt=seeMe.indexOf(";"); 
      alert("Hello there, " + seeMe) 
<body onLoad="document.baker.reset( )"> 
<div ID=display>&nbsp; 
<form name="baker"> 
      &nbsp;<Input type=text Name="myCookie" ><br>&nbsp; 
      Type in the value for your cookie and click the button to set the cookie<BR> 
      &nbsp;<input type=button value="Bake Cookie" 
      Click the button to read the value of the cookie along with a greeting. <br> 
      &nbsp;<input type=button value="See Cookie" onclick="welcomeBack( );"><br> 




Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint