Standard cookie functions: extract Cookie Value : Cookie « Development « JavaScript DHTML






Standard cookie functions: extract Cookie Value

  
<html>
<script language="JavaScript">
<!--
var visits = 0;

function extractCookieValue(val) {
  if ((endOfCookie = document.cookie.indexOf(";", val)) == -1) {
     endOfCookie = document.cookie.length;
  }
  return unescape(document.cookie.substring(val,endOfCookie));
}

function ReadCookie(cookiename) {
  var numOfCookies = document.cookie.length;
  var nameOfCookie = cookiename + "=";
  var cookieLen = nameOfCookie.length;
  var x = 0;
  while (x <= numOfCookies) {
        var y = (x + cookieLen);
        if (document.cookie.substring(x, y) == nameOfCookie)
           return (extractCookieValue(y));
           x = document.cookie.indexOf(" ", x) + 1;
           if (x == 0){
              break;
           }
  }
  return (null);
}

function createCookie(name, value, expiredays) {
  var todayDate = new Date();
  todayDate.setDate(todayDate.getDate() + expiredays);
  document.cookie = name + "=" + value + "; expires=" +todayDate.toGMTString() + ";"
}

function showHits() {
  userCookie = ReadCookie("_visitSite");
  if (userCookie == null)
     visits = 1;
  else
     visits = parseInt(userCookie) + 1;
     createCookie("_visitSite", visits, 30);
     document.write("You have visited this site on <b>" +visits + "</b> occasions<hr>");
}
//-->
</script>
</head>
<body>
<script>
showHits();
</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.Save name to cookie
8.Cookie set, delete, get value and create
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