部分script cookies 操作

本文介绍了一组JavaScript函数,用于处理浏览器的Cookies。包括设置、获取、删除Cookies及检查Cookies的存在状态等实用功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值