Cookie set, delete, get value and create : Cookie « Development « JavaScript DHTML

Home
JavaScript DHTML
1.Ajax Layer
2.Data Type
3.Date Time
4.Development
5.Document
6.Dojo toolkit
7.Event
8.Event onMethod
9.Ext JS
10.Form Control
11.GUI Components
12.HTML
13.Javascript Collections
14.Javascript Objects
15.Javascript Properties
16.jQuery
17.Language Basics
18.Mochkit
19.Mootools
20.Node Operation
21.Object Oriented
22.Page Components
23.Rico
24.Scriptaculous
25.Security
26.SmartClient
27.Style Layout
28.Table
29.Utilities
30.Window Browser
31.YUI Library
JavaScript DHTML » Development » Cookie 
Cookie set, delete, get value and create
  
/*
JavaScript Application Cookbook
By Jerry Bradenbaugh

Publisher: O'Reilly 
Series: Cookbooks
ISBN: 1-56592-577-7
*/ 

 
<HTML>
<HEAD>
<TITLE>cookie set, delete, get value and create</TITLE>
<SCRIPT LANUAGE="JavaScript">
// cookies.js
// Derived from the Bill Dortch code at http://www.hidaho.com/cookies/cookie.txt

var today = new Date();
var expiry = new Date(today.getTime() 365 24 60 60 1000);

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1) { endstr = document.cookie.length; }
  return unescape(document.cookie.substring(offset, endstr));
  }

function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j== arg) {
      return getCookieVal (j);
      }
    i = document.cookie.indexOf(" ", i1;
    if (i == 0break
    }
  return null;
  }

function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
    ((path"; path=" + path : ""+
    ((domain"; domain=" + domain : ""+
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
  }

function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value+
    ((expires"; expires=" + expires.toGMTString() ""+
    ((path"; path=" + path : ""+
    ((domain"; domain=" + domain : ""+
    ((secure"; secure" "");
  }
</SCRIPT>

</HEAD>
<BODY BGCOLOR=WHITE>

<SCRIPT LANGUAGE="JavaScript">
<!--

if (GetCookie('user_id') == null) {
  var getName = prompt('Hi... First time, huh?? We all have to  through it. Please enter your name.', '');
  document.writeln('<H2>Welcome, ' + (getName != '' ? getName : 'Anonymous user') '</H2>' + 
    'This is your first visit.');
  SetCookie('user_id', (getName != '' ? getName : 'Anonymous user'), expiry);
  SetCookie('hit_count', '2', expiry);
  }
else {
  var getName = GetCookie('user_id');
  var getHits = GetCookie('hit_count');
  document.writeln('<H2>Welcome Back, ' + getName + '</H2>' + 
    'You have visited ' + getHits + ' times.');
  getHits = parseInt(getHits1;
  SetCookie('hit_count', '' + getHits + '', expiry);
  }

//-->
</SCRIPT>

</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 utility function
10.Cookie install and delete (remove)
11.Cookie: retrieve a future expiration date in proper format
12.A Cookie Example
13.A Cookie Test Program
14.Quiz Program base on Cookie
15. A Website Access Counter
16. Keeping Track of User Access Time
17.Bill Dortch's Cookie Functions
18.Cookie Preferences
19.Set cookie to document and read it back
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.