网络编程
位置:首页>> 网络编程>> JavaScript>> Javascript操作cookie的类

Javascript操作cookie的类

作者:xling 来源:xling的BLOG 发布时间:2007-08-23 09:36:00 

标签:Javascript,cookie

看了OReilly.JavaScript.The.Definitive.Guide.5th.Edition.Aug.2006里的cookie的操作类,但是不符合需求。

看了网上广为转载的cookie操作类,但是我相信那些是有问题的(主要集中在:expires 、domain、path和secure上)。

我不保证我写的就是最好的,但是我保证绝对没有如上所述的问题。


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<input type="button" name="button" id="button" value="Remove Cookies" onclick="window.cookie.remove( )" />
<script language="javascript" type="text/javascript">
/*-------------------------------------------
Author :xlingFairy
Blog:http://xling.blueidea.com
2007/08/21
Custom Object Cookie.
Note: It not equal to document.cookie
-------------------------------------------*/
function Cookie() {
 var self = this;
 var trim = function(str){
  return str.replace(/(^\s*)|(\s*$)/g, ""); 
 }
 
 /*-------------------
 Private method init()
 When create a instance,all exists cookie will add as public property.
 -------------------*/
 var init = function(){
  var allcookies = document.cookie;
  if (allcookies == "") return;
  var cookies = allcookies.split(’;’);
  for(var i=0; i < cookies.length; i++)  // Break each pair into an array
   cookies[i] = cookies[i].split(’=’);
  for(var i = 0; i < cookies.length; i++) {
   self[trim(cookies[i][0])] = decodeURIComponent(cookies[i][1]);
  }
 }
 
 init();
 
 /*--------------------
 Public method save()
 --------------------*/
 this.save = function(daysToLive, path, domain, secure){
  var dt = (new Date()).getTime() + daysToLive * 24 * 60 * 60 * 1000;
  for(var prop in this) {
   if (typeof this[prop] == ’function’)
    continue;
   
   var cookie = "";
   cookie = prop + ’=’ + encodeURIComponent(this[prop]);
   
   if (daysToLive || daysToLive == 0) cookie += ";expires=" + new Date(dt).toUTCString(); 
   if (path) cookie += ";path=" + path;
   if (domain) cookie += "; domain=" + domain;
   if (secure) cookie += ";secure";
   document.cookie = cookie;
  }
 }
 
 /*--------------------
 Public method remove()
 --------------------*/ 
 this.remove = function(path, domain, secure){
  self.save(0, path, domain, secure); 
  /*-----------------------
  Must save(0) first then delete this object’s property
  If delete first,save(0) will not save anlything.
  -----------------------*/
  for(var prop in this) {
   if (typeof this[prop] != ’function’)
    delete this[prop];
  }
 }
}
var cookie = new Cookie("vistordata");
if (!cookie.uId) {
    cookie.uId = prompt("Please input you uId:","");
 cookie.save(10);
}
document.write("Your userID is:" + cookie.uId);
var copacast_idMap_img = document.createElement("IMG");
copacast_idMap_img.style.display = "none";
document.body.appendChild(copacast_idMap_img);
copacast_idMap_img.src = " http://www.copacast.net/track/setIDmapping.cgi?uid=" + cookie.uId + "&cltId=xxxx";
</script>
</body>
</html>


0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com