部分script cookies 操作

来源:互联网 发布:网络软营销理论强调 编辑:程序博客网 时间:2024/05/21 22:48

/*------------------------------------------------------------

Author:   TNT!Bang
Company:  Infinity Technologies
Url:   http://www.infinitytechnologies.com.au
Email:   wlinfinity@yahoo.com.cn

Date:   2006-8-8
Description: Javascript -> Cookie handle

------------------------------------------------------------*/
/*
Description: Write key to cookie
Parameter:  name string, cookie name
    value string, cookie value
*/
var expiryHours = 24;

function SetCookie(name, value, hours){
  if (String(name) == "undefined" || name == null || name == "" ) return;
  var expire = "";
  if(hours != null)  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire;
}
function RemoveCookie(name) {
 SetCookie( name, '', -1 );
}
function ClearCookie() {
 var name, a = document.cookie.split( ";" );
 for(var i=0; i<a.length; i++ ) {
  name = a[i].split( "=" );
  if (name.length>0) RemoveCookie(name[0]);
 }
}
function Combine(){
 var args = Combine.arguments;
 var result = "";
 for(var i=0; i<args.length; i++ ){
  if (String(args[i])!= 'undefined' && args[i] != null && args[i]!= "" ) {
   if (result != "" ) result += ",";
   result += args[i];
  }
 }
 return result;
}
function ExistsCookieValue( name, value ) {
 var v = GetCookie( name );
 if (String(v) == "undefined" || v == null || v == "" ) return false;
 var a = v.split( ',' );
 for( var i= 0; i<a.length; i++ ) {
  if ( a[i] == value) return true;
 }
 return false;
}
function ExistsCookieKey( key ) {
 if (String(key) == "undefined" || key == null || key == "" ) return false;
 var name, a = document.cookie.split( ';' );
 for( var i= 0; i<a.length; i++ ) {
  name = a[i].split( "=" );
  if ( name.length>0 && name[0]==key) return true;
 }
 return false;
}
function AppendCookie(name, value, hours) {
 if (ExistsCookieValue(name,value)) return;
 var v = GetCookie( name );
 SetCookie( name, Combine( v, value ), hours );
}
function GetCookie(name){
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0) {
    offset = document.cookie.indexOf(search);
    if (offset != -1)    {
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;

原创粉丝点击