Home  >  Article  >  Web Front-end  >  javascript js cookie的存储,获取和删除_javascript技巧

javascript js cookie的存储,获取和删除_javascript技巧

WBOY
WBOYOriginal
2016-05-16 19:06:52856browse

使用方法:

//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');


函数如下:

复制代码 代码如下:

<script> <BR>/**//************************************************************************ <BR>| 函数名称: setCookie | <BR>| 函数功能: 设置cookie函数 | <BR>| 入口参数: name:cookie名称;value:cookie值 | <BR>| 维护记录: Spark(创建) | <BR>| 版权所有: (C) 2006-2007 北京东方常智科技有限公司 | <BR>| 编写时间: 2007年9月13日 21:00 | <BR>*************************************************************************/ <BR>function setCookie(name, value) <BR>...{ <BR> var argv = setCookie.arguments; <BR> var argc = setCookie.arguments.length; <BR> var expires = (argc > 2) ? argv[2] : null; <BR> if(expires!=null) <BR> ...{ <BR> var LargeExpDate = new Date (); <BR> LargeExpDate.setTime(LargeExpDate.getTime() + (expires*1000*3600*24)); <BR> } <BR> document.cookie = name + "=" + escape (value)+((expires == null) ? "" : ("; expires=" +LargeExpDate.toGMTString())); <BR>} <BR>/**//************************************************************************ <BR>| 函数名称: getCookie | <BR>| 函数功能: 读取cookie函数 | <BR>| 入口参数: Name:cookie名称 | <BR>| 维护记录: Spark(创建) | <BR>| 版权所有: (C) 2006-2007 北京东方常智科技有限公司 | <BR>| 编写时间: 2007年9月13日 21:02 | <BR>*************************************************************************/ <BR>function getCookie(Name) <BR>...{ <BR> var search = Name + "=" <BR> if(document.cookie.length > 0) <BR> ...{ <BR> offset = document.cookie.indexOf(search) <BR> if(offset != -1) <BR> ...{ <BR> offset += search.length <BR> end = document.cookie.indexOf(";", offset) <BR> if(end == -1) end = document.cookie.length <BR> return unescape(document.cookie.substring(offset, end)) <BR> } <BR> else return "" <BR> } <BR>} <br><br>/**//************************************************************************ <BR>| 函数名称: deleteCookie | <BR>| 函数功能: 删除cookie函数 | <BR>| 入口参数: Name:cookie名称 | <BR>| 维护记录: Spark(创建) | <BR>| 版权所有: (C) 2006-2007 北京东方常智科技有限公司 | <BR>| 编写时间: 2007年9月15日 18:10 | <BR>*************************************************************************/ <BR>function deleteCookie(name) <BR>...{ <BR> var expdate = new Date(); <BR> expdate.setTime(expdate.getTime() - (86400 * 1000 * 1)); <BR> setCookie(name, "", expdate); <BR>} <BR></script>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn