写入cookie的JavaScript代码库 cookieLibrary.js
发布时间:2024-04-16 10:41:08
/* 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>");


猜你喜欢
- 图例如下1.先在detail.html中做好页面上下文链接;然后在view.py中进行数据绑定:2.访问验证以上来源:https://www
- 最近因为要写一个项目的接口,需要远程的连接oracle数据库,刚开始的时候因为我本地只装了MySQL,所以用就连接了本地MySQL,接口大体
- 本文较为详细的讲述了Python实现远程调用MetaSploit的方法,对Python的学习来说有很好的参考价值。具体实现方法如下:(1)安
- 在使用Python做开发的时候,时不时会给自己编写了一些小工具辅助自己的工作,但是由于开发依赖环境问题,多数只能在自己电脑上运行,拿到其它电
- 原文地址:30 Days of Mootools 1.2 Tutorials - Day 18 - Classes part IClass(
- 今天的这篇文章呢,小编来介绍一下如何通过Python来创建各种形式的文件,这里包括了文本文件CSV文件Excel文件压缩文件XML文件JSO
- 访问phpmyadmin时总是出现 “无法载入 mysql 扩展,请检查 PHP 配置”。
- 背景在python工程完成开发以后需要编译成可执行文件,如此一来生产环境和开发环境隔离开来便于用户使用(可独立使用,无需配置python开发
- time 模块主要包含各种提供日期、时间功能的类和函数。该模块既提供了把日期、时间格式化为字符串的功能,也提供了从字符串恢复日期、时间的功能
- --为空的值text ntext select * from lf_newsNg_utf where datalength(newsCont
- 都知道最近ChatGPT聊天机器人爆火,我也想方设法注册了账号,据说后面要收费了。ChatGPT是一种基于大语言模型的生成式AI,换句话说它
- #!/usr/bin/python# -*- coding: utf-8 -*-from scapy.all import *from ti
- MySQL中的事件调度器,EVENT,也叫定时任务,类似于Unix crontab或Windows任务调度程序。EVENT由其名称和所在的s
- 代码之余,将代码过程重要的一些代码段备份一下,如下的代码内容是关于Python从ftp服务器下载文件的的代码,希望能对小伙伴有用途。#cod
- 我们网站的静态资源(css、js和背景图片)和web应用程序是分开部署的,几乎所有的静态资源都部署在同一个应用下。最开始的网站
- 引言所有的层都具有的参数,如name, type, bottom, top和transform_param请参看我的前一篇文章:Caffe卷
- 删除重复记录,将TABLE_NAME中的不重复记录保存到#TABLE_NAME中 select distinct *&n
- 在做我的友情链接批量检查工具过程中,碰到一些情况,就是对方网页会用gzip压缩。用gzip压缩的好处是,能压缩网页大小,加快网页的浏览速度,
- matplotlib官方除了提供了鼠标十字光标的示例,还提供了同一图像内多子图共享光标的示例,其功能主要由widgets模块中的MultiC
- 1:把数字转换为字符串的方法 var string_value = String(numbe