Set cookie to document and read it back : Cookie « Development « JavaScript DHTML






Set cookie to document and read it back

  
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
function setCookie(){
  var thename= "your name";
  var the_text="name="+thename+"&";
  var toexpire= new Date("March 31, 2010");
  var expdate="expires="+toexpire.toGMTString();
  the_text+=expdate;
 
  var newtext=escape(the_text);
  document.cookie=newtext;


  var mycookie=document.cookie;
  var fixed_cookie= unescape(mycookie);
  var thepairs= fixed_cookie.split("&");
  var pair1= thepairs[0];
  var pair2= thepairs[1];
  var namevalue= pair1.split("=");
  window.alert("Welcome, "+namevalue[1]+"!");
 
}
setCookie();
</SCRIPT>
</HEAD>
<BODY>
text
</BODY>
</HTML>

   
    
  








Related examples in the same category

1.Reads, writes and deletes current Web page's cookies
2.'cookieEnabled' Example
3.Create a cookie
4.Set the cookie expire date
5.Secure cookie
6.Read all cookies
7.Standard cookie functions: extract Cookie Value
8.Save name to cookie
9.Cookie set, delete, get value and create
10.Cookie utility function
11.Cookie install and delete (remove)
12.Cookie: retrieve a future expiration date in proper format
13.A Cookie Example
14.A Cookie Test Program
15.Quiz Program base on Cookie
16. A Website Access Counter
17. Keeping Track of User Access Time
18.Bill Dortch's Cookie Functions
19.Cookie Preferences