写入cookie的JavaScript代码库 cookieLibrary.js

/* Cookie Library -- "Night of the Living Cookie" Version (25-Jul-96)
2缔友计算机信息技术有限公司,涂聚文 geovindu@163.com 互相交流
3 Written by: Bill Dortch, hIdaho Design <geovindu@163.com>
4 The following functions are released to the public domain.
5http://www.dusystem.com/
6 This version takes a more aggressive approach to deleting
7 cookies. Previous versions set the expiration date to one
8 millisecond prior to the current time; however, this method
9 did not work in Netscape 2.02 (though it does in earlier and
later versions), resulting in "zombie" cookies that would not
die. DeleteCookie now sets the expiration date to the earliest
usable date (one second into 1970), and sets the cookie's value
to null for good measure.

Also, this version adds optional path and domain parameters to
the DeleteCookie function. If you specify a path and/or domain
when creating (setting) a cookie**, you must specify the same
path/domain when deleting it, or deletion will not occur.

The FixCookieDate function must now be called explicitly to
correct for the 2.x Mac date bug. This function should be
called *once* after a Date object is created and before it
is passed (as an expiration date) to SetCookie. Because the
Mac date bug affects all dates, not just those passed to
SetCookie, you might want to make it a habit to call
FixCookieDate any time you create a new Date object:

var theDate = new Date();
FixCookieDate (theDate);

Calling FixCookieDate has no effect on platforms other than
the Mac, so there is no need to determine the user's platform
prior to calling it.

This version also incorporates several minor coding improvements.

**Note that it is possible to set multiple cookies with the same
name but different (nested) paths. For example:

SetCookie ("color","red",null,"/outer");
SetCookie ("color","blue",null,"/outer/inner");

However, GetCookie cannot distinguish between these and will return
the first cookie that matches a given name. It is therefore
recommended that you *not* use the same name for cookies with
different paths. (Bear in mind that there is *always* a path
associated with a cookie; if you don't explicitly specify one,
the path of the setting document is used.)

Revision History:

"Toss Your Cookies" Version (22-Mar-96)
- Added FixCookieDate() function to correct for Mac date bug

"Second Helping" Version (21-Jan-96)
- Added path, domain and secure parameters to SetCookie
- Replaced home-rolled encode/decode functions with Netscape's
new (then) escape and unescape functions

"Free Cookies" Version (December 95)

For information on the significance of cookie parameters, and
and on cookies in general, please refer to the official cookie
spec, at:

http:www.netscape.com/newsref/std/cookie_spec.html

****************************************************************** */

/**//* "Internal" function to return the decoded value of a cookie */


代码如下:

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

/**//* Function to correct for 2.x Mac date bug. Call this function to
fix a date object prior to passing it to SetCookie.
IMPORTANT: This function should only be called *once* for
any given date object! See example at the end of this document. */


代码如下:

function FixCookieDate (date) {
var base = new Date(0);
var skew = base.getTime(); // dawn of (Unix) time - should be 0
if (skew > 0) { // except on the Mac - ahead of its time
date.setTime(date.getTime() - skew);
}
}

/**//* Function to return the value of the cookie specified by "name".
name - String object containing the cookie name.
returns - String object containing the cookie value, or null if
the cookie does not exist. */


代码如下:

function GetCookie (name) {
var temp = name + "=";
var tempLen = temp.length;
var cookieLen = document.cookie.length;
var i = 0;
while (i < cookieLen) {
var j = i + tempLen;
if (document.cookie.substring(i, j) == temp) {
return getCookieVal(j);
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

/**//* Function to create or update a cookie.
name - String object containing the cookie name.
value - String object containing the cookie value. May contain
any valid string characters.
[expiresDate] - Date object containing the expiration data of the cookie. If
omitted or null, expires the cookie at the end of the current session.
[path] - String object indicating the path for which the cookie is valid.
If omitted or null, uses the path of the calling document.
[domain] - String object indicating the domain for which the cookie is
valid. If omitted or null, uses the domain of the calling document.
[secure] - Boolean (true/false) value indicating whether cookie transmission
requires a secure channel (HTTPS).

The first two parameters are required. The others, if supplied, must
be passed in the order listed above. To omit an unused optional field,
use null as a place holder. For example, to call SetCookie using name,
value and path, you would code:

SetCookie ("myCookieName", "myCookieValue", null, "/");

Note that trailing omitted parameters do not require a placeholder.

To set a secure cookie for path "/myPath", that expires after the
current session, you might code:

SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true); */


代码如下:

function SetCookie (name,value,expiresDate,path,domain,secure) {
document.cookie = name + "=" + escape (value) +
((expiresDate) ? "; expires=" + expiresDate.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}

/**//* Function to delete a cookie. (Sets expiration date to start of epoch)
name - String object containing the cookie name
path - String object containing the path of the cookie to delete. This MUST
be the same as the path used to create the cookie, or null/omitted if
no path was specified when creating the cookie.
domain - String object containing the domain of the cookie to delete. This MUST
be the same as the domain used to create the cookie, or null/omitted if
no domain was specified when creating the cookie. */


代码如下:

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";
}
}

// Calling examples:
// var expdate = new Date ();
// FixCookieDate (expdate); // Correct for Mac date bug - call only once for given Date object!
// expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000)); // 24 hrs from now
// SetCookie ("ccpath", "http://www.dupcit.com/articles/", expdate);
// SetCookie ("ccname", "WebWoman", expdate);
// SetCookie ("tempvar", "This is a temporary cookie.");
// SetCookie ("ubiquitous", "This cookie will work anywhere in this domain",null,"/");
// SetCookie ("paranoid", "This cookie requires secure communications",expdate,"/",null,true);
// SetCookie ("goner", "This cookie must die!");
// document.write (document.cookie + "<br>");
// DeleteCookie ("goner");
// document.write (document.cookie + "<br>");
// document.write ("ccpath = " + GetCookie("ccpath") + "<br>");
// document.write ("ccname = " + GetCookie("ccname") + "<br>");
// document.write ("tempvar = " + GetCookie("tempvar") + "<br>");

(0)

相关推荐

  • javascript js cookie的存储,获取和删除

    使用方法: //1.存储Cookie //2.参数说明: 1.参数1:Cookie存储Name,参数2:Cookie要存储的值 //3.例子如下: setCookie('Method',match); //1.获取Cookie //2.参数说明: 1.参数1:Cookie存储的Name //3.例子如下: getCookie('Method') //1.删除Cookie //2.参数说明: 1.参数1:Cookie存储的Name //3.例子如下: deleteCookie('Method');

  • JavaScript使用cookie

    cookie概述 在上一节,曾经利用一个不变的框架来存储购物栏数据,而商品显示页面是不断变化的,尽管这样能达到一个模拟全局变量的功能,但并不严谨.例如在导航框架页面内右击,单击快捷菜单中的[刷新]命令,则所有的JavaScript变量都会丢失.因此,要实现严格的跨页面全局变量,这种方式是不行的,JavaScript中的另一个机制:cookie,则可以达到真正全局变量的要求. cookie是浏览器提供的一种机制,它将document对象的cookie属性提供给JavaScript.可以由JavaS

  • javascript cookies操作集合

    复制代码 代码如下: function SetCookie(sName, sValue) { date = new Date(); var str=sName+"="+escape(sValue)+(";expires="+date.toGMTString())+";path=/"; str=str.replace("2010","2099"); document.cookie=str; alert(&qu

  • JavaScript Cookie的读取和写入函数

    Html代码: 复制代码 代码如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

  • javascript操作cookie的文章(设置,删除cookies)

    下面这篇是国外的一篇文章.http://www.jb51.net/article/20553.htm 复制代码 代码如下: var sel = new Object(); var sel_num = 0; function getCookieVal(offset) { var endstr = document.cookie.indexOf(";", offset); if (endstr == -1) endstr = document.cookie.length; return u

  • javascript 24小时弹出一次的代码(利用cookies)

    复制代码 代码如下: function jb51tuitan(){ if (getCookie('jb51popped')==''){ //要执行的代码或一些操作setCookie("jb51popped","ok"); //写入cookies表示已经执行过了. alert("ok"); } } jb51tuitan() function setCookie(name, value)     {     var argv = setCookie.

  • Javascript Cookie读写删除操作的函数

    复制代码 代码如下: function getCookie( name ) { var start = document.cookie.indexOf( name + "=" ); var len = start + name.length + 1; if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) { return null; } if ( start == -1

  • javascript cookies 设置、读取、删除实例代码

    刚整理了一些关于javascript cookies操作的文章,发现这篇文章也不错,推荐大家一起参考,选择需要的,不足的地方主要是对路径的设置,喜欢的朋友可以结合下. 复制代码 代码如下: <script> function SetCookie(name,value)//两个参数,一个是cookie的名子,一个是值 { var Days = 30; //此 cookie 将被保存 30 天 var exp = new Date(); //new Date("December 31,

  • Javascript操作cookie的函数代码

    javascript操作cookie简单版本 复制代码 代码如下: function setCookie(name, value, iDay) { var oDate = new Date(); oDate.setDate(oDate.getDate() + iDay); document.cookie = name+'='+value+';expires='+oDate; } function getCookie(name) { var arr = document.cookie.split(

  • javascript实现操作cookie实现的可记忆菜单

    可记忆菜单 body {font:12px Tahoma, sans-serif;color:#4D4D4D;margin:0px; text-align:center} a{color:#4D4D4D;text-decoration:none} a:hover{color:#AD0000;text-decoration:none} #menu {width:160px;margin:0px;padding:0px;text-align:left;list-style:none;border:1

  • JavaScript cookie的设置获取删除详解

    设置cookie 每个cookie都是一个名/值对,可以把下面这样一个字符串赋值给document.cookie: document.cookie="userId=828"; 如果要一次存储多个名/值对,可以使用分号加空格(; )隔开,例如: document.cookie="userId=828; userName=hulk"; 在cookie的名或值中不能使用分号(;).逗号(,).等号(=)以及空格.在cookie的名中做到这点很容易,但要保存的值是不确定的.

  • JavaScript实现cookie的写入、读取、删除功能

    在没介绍正文之前,先给大家介绍Cookie的基础知识 首先了解什么是cookie "cookie 是存储于访问者的计算机中的变量.每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie.你可以使用 JavaScript 来创建和取回 cookie 的值." cookie 是访问过的网站创建的文件,用于存储浏览信息,例如个人资料信息. 从JavaScript的角度看,cookie 就是一些字符串信息.这些信息存放在客户端的计算机中,用于客户端计算机与服务器之间传递信息. 在

  • JavaScript 保存数组到Cookie的代码

    JavaScript中数组是无法直接保存为Cookie的(PHP可以),那要将数组转存为字符串,再保存在Cookie中,简单的一维数组我们直接用toString()或者join就可以了: JavaScript中toString函数方法是返回对象的字符串表示. 使用方法:objectname.toString([radix]) 其中objectname是必选项.要得到字符串表示的对象. radix是可选项.指定将数字值转换为字符串时的进制. join是其中一个方法. 格式:objArray.joi

  • Javascript读取cookie函数代码

    用法: 一.设置cookie 复制代码 代码如下: var cookie = new JSCookie(); // 普通设置 cookie .SetCookie("key1","val1"); // 过期时间为一年 var expire_time = new Date(); expire_time.setFullYear(expire_time.getFullYear() + 1); cookie .SetCookie("key2","

随机推荐