您的当前位置:首页正文

js文件Cookie存取值示例代码_javascript技巧

2023-12-04 来源:帮我找美食网

代码如下: /* Cookie工具 使用方法: //存值 var value = "7天"; tools.cookie("day",value, {expires:7}); //将字符串:"7天" 以 "day"这个key保存到cookie中5天 //取值 var v = tools.cookie("day"); //用 "day" 这个key从cookie取出值 */ tools.cookie = function(name, value, options) { if (typeof value != 'undefined') { // name and value given, set cookie options = options || {}; if (value === null) { value = ''; options.expires = -1; } var expires = ''; if (options.expires && (typeof options.expires == 'number' || options.expires.toGMTString)) { var date; if (typeof options.expires == 'number') { date = new Date(); date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); } else { date = options.expires; } expires = '; expires=' + date.toGMTString(); // use expires // attribute, // max-age is not // supported by IE } var path = options.path ? '; path=' + options.path : ''; var domain = options.domain ? '; domain=' + options.domain : ''; var secure = options.secure ? '; secure' : ''; document.cookie = [ name, '=', encodeURIComponent(value), expires, path, domain, secure ].join(''); } else { // only name given, get cookie var cookieValue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for ( var i = 0; i < cookies.length; i++) { var cookie = jQuery.trim(cookies[i]); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) == (name + '=')) { cookieValue = decodeURIComponent(cookie .substring(name.length + 1)); break; } } } return cookieValue; } };

小编还为您整理了以下内容,可能对您也有帮助:

如何用js向cookie中保存数据、取数据?

用js向cookie中保存数据、获取数据的方法如下:x0dx0afunction GetCookieVal(offset)x0dx0a//获得Cookie解码后的值x0dx0a{x0dx0avar endstr = document.cookie.indexOf (";", offset);x0dx0aif (endstr == -1)x0dx0aendstr = document.cookie.length;x0dx0areturn unescape(document.cookie.substring(offset, endstr));x0dx0a}x0dx0ax0dx0a//---------------------------x0dx0afunction SetCookie(name, value)x0dx0a//设定Cookie值x0dx0a{x0dx0avar expdate = new Date();x0dx0avar argv = SetCookie.arguments;x0dx0avar argc = SetCookie.arguments.length;x0dx0avar expires = (argc > 2) ? argv[2] : null;x0dx0avar path = (argc > 3) ? argv[3] : null;x0dx0avar domain = (argc > 4) ? argv[4] : null;x0dx0avar secure = (argc > 5) ? argv[5] : false;x0dx0aif(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));x0dx0adocument.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("; expires="+ expdate.toGMTString()))x0dx0a+((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))x0dx0a+((secure == true) ? "; secure" : "");x0dx0a}x0dx0ax0dx0a//---------------------------------x0dx0afunction DelCookie(name)x0dx0a//删除Cookiex0dx0a{x0dx0avar exp = new Date();x0dx0aexp.setTime (exp.getTime() - 1);x0dx0avar cval = GetCookie (name);x0dx0adocument.cookie = name + "=" + cval + "; expires="+ exp.toGMTString();x0dx0a}x0dx0ax0dx0a//------------------------------------x0dx0afunction GetCookie(name)x0dx0a//获得Cookie的原始值x0dx0a{x0dx0avar arg = name + "=";x0dx0avar alen = arg.length;x0dx0avar clen = document.cookie.length;x0dx0avar i = 0;x0dx0awhile (i < clen)x0dx0a{x0dx0avar j = i + alen;x0dx0aif (document.cookie.substring(i, j) == arg)x0dx0areturn GetCookieVal (j);x0dx0ai = document.cookie.indexOf(" ", i) + 1;x0dx0aif (i == 0) break;x0dx0a}x0dx0areturn null;x0dx0a}x0dx0ax0dx0aSetCookie("username1",99);x0dx0aalert(GetCookie("username1"));

js文件Cookie存取值的使用

这篇文章主要介绍了js文件Cookie存取值的使用,需要的朋友可以参考下
代码如下:
/*
Cookie工具
使用方法:
//存值
var
value
=
"7天";
tools.cookie("day",value,
{expires:7});
//将字符串:"7天"

"day"这个key保存到cookie中5天
//取值
var
v
=
tools.cookie("day");
//用
"day"
这个key从cookie取出值
*/
tools.cookie
=
function(name,
value,
options)
{
if
(typeof
value
!=
'undefined')
{
//
name
and
value
given,
set
cookie
options
=
options
||
{};
if
(value
===
null)
{
value
=
'';
options.expires
=
-1;
}
var
expires
=
'';
if
(options.expires
&&
(typeof
options.expires
==
'number'
||
options.expires.toGMTString))
{
var
date;
if
(typeof
options.expires
==
'number')
{
date
=
new
Date();
date.setTime(date.getTime()
+
(options.expires
*
24
*
60
*
60
*
1000));
}
else
{
date
=
options.expires;
}
expires
=
';
expires='
+
date.toGMTString();
//
use
expires
//
attribute,
//
max-age
is
not
//
supported
by
IE
}
var
path
=
options.path
?
';
path='
+
options.path
:
'';
var
domain
=
options.domain
?
';
domain='
+
options.domain
:
'';
var
secure
=
options.secure
?
';
secure'
:
'';
document.cookie
=
[
name,
'=',
encodeURIComponent(value),
expires,
path,
domain,
secure
].join('');
}
else
{
//
only
name
given,
get
cookie
var
cookieValue
=
null;
if
(document.cookie
&&
document.cookie
!=
'')
{
var
cookies
=
document.cookie.split(';');
for
(
var
i
=
0;
i
<
cookies.length;
i++)
{
var
cookie
=
jQuery.trim(cookies[i]);
//
Does
this
cookie
string
begin
with
the
name
we
want?
if
(cookie.substring(0,
name.length
+
1)
==
(name
+
'='))
{
cookieValue
=
decodeURIComponent(cookie
.substring(name.length
+
1));
break;
}
}
}
return
cookieValue;
}
};

js文件Cookie存取值的使用

这篇文章主要介绍了js文件Cookie存取值的使用,需要的朋友可以参考下
代码如下:
/*
Cookie工具
使用方法:
//存值
var
value
=
"7天";
tools.cookie("day",value,
{expires:7});
//将字符串:"7天"

"day"这个key保存到cookie中5天
//取值
var
v
=
tools.cookie("day");
//用
"day"
这个key从cookie取出值
*/
tools.cookie
=
function(name,
value,
options)
{
if
(typeof
value
!=
'undefined')
{
//
name
and
value
given,
set
cookie
options
=
options
||
{};
if
(value
===
null)
{
value
=
'';
options.expires
=
-1;
}
var
expires
=
'';
if
(options.expires
&&
(typeof
options.expires
==
'number'
||
options.expires.toGMTString))
{
var
date;
if
(typeof
options.expires
==
'number')
{
date
=
new
Date();
date.setTime(date.getTime()
+
(options.expires
*
24
*
60
*
60
*
1000));
}
else
{
date
=
options.expires;
}
expires
=
';
expires='
+
date.toGMTString();
//
use
expires
//
attribute,
//
max-age
is
not
//
supported
by
IE
}
var
path
=
options.path
?
';
path='
+
options.path
:
'';
var
domain
=
options.domain
?
';
domain='
+
options.domain
:
'';
var
secure
=
options.secure
?
';
secure'
:
'';
document.cookie
=
[
name,
'=',
encodeURIComponent(value),
expires,
path,
domain,
secure
].join('');
}
else
{
//
only
name
given,
get
cookie
var
cookieValue
=
null;
if
(document.cookie
&&
document.cookie
!=
'')
{
var
cookies
=
document.cookie.split(';');
for
(
var
i
=
0;
i
<
cookies.length;
i++)
{
var
cookie
=
jQuery.trim(cookies[i]);
//
Does
this
cookie
string
begin
with
the
name
we
want?
if
(cookie.substring(0,
name.length
+
1)
==
(name
+
'='))
{
cookieValue
=
decodeURIComponent(cookie
.substring(name.length
+
1));
break;
}
}
}
return
cookieValue;
}
};

js中如何获取Cookies的值

首先JS设置cookie:

假设在A页面中要保存变量username的值("jack")到cookie中,key值为name,则相应的JS代码为:

document.cookie="name="+username;

JS读取cookie:

var username=document.cookie.split(";")[0].split("=")[1];

function setCookie(name,value)

{

var Days = 30;

var exp = new Date();

exp.setTime(exp.getTime() + Days*24*60*60*1000);

document.cookie = name +"="+ escape (value) +";expires=" + exp.toGMTString();

}

读取cookies

function getCookie(name)

{

var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");

if(arr=document.cookie.match(reg))

return unescape(arr[2]);

else

return null;

}

扩展资料

服务器可以利用Cookies包含信息的任意性来筛选并经常性维护这些信息,以判断在HTTP传输中的状态。Cookies最典型的应用是判定注册用户是否已经登录网站,用户可能会得到提示,是否在下一次进入此网站时保留用户信息以便简化登录手续,这些都是Cookies的功用。

另一个重要应用场合是“购物车”之类处理。用户可能会在一段时间内在同一家网站的不同页面中选择不同的商品,这些信息都会写入Cookies,以便在最后付款时提取信息。

如何使用js来获取cookie的值

<!DOCTYPE HTML>

<html lang="en-US">

<head>

<meta charset="UTF-8">

<meta name="keywords" content="白菜编辑部">

<title>白菜编辑部</title>

<style type="text/css">

</style>

<script type="text/javascript">

function readCookie (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;

    }

    function writeCookie (name, value, hours)

    {

    var expire = "";

    if (hours != null)

    {

    expire = new Date ((new Date ()).getTime () + hours * 3600000);

    expire = "; expires=" + expire.toGMTString ();

    }

    document.cookie = name + "=" + escape (value) + expire;

    }

    

    writeCookie ("myCookie", "my name", 24);

    alert (readCookie ("myCookie"));

</script>

</head>

<body>

</body>

</html>

帮我找美食网还为您提供以下相关内容希望对您有帮助:

js文件Cookie存取值的使用

value = "7天";tools.cookie("day",value,{expires:7});//将字符串:"7天"以 "day"这个key保存到cookie中5天 //取值 var v = tools.cookie("day");//用 "day"这个key从cookie取出值 / tools.cookie = functi...

如何用js向cookie中保存数据、取数据?

function GetCookieVal(offset)//获得Cookie解码后的值 { var endstr = document.cookie.indexOf (";", offset);if (endstr == -1)endstr = document.cookie.length;return unescape(document.cookie.substring(offset,...

js中如何获取Cookies的值

假设在A页面中要保存变量username的值("jack")到cookie中,key值为name,则相应的JS代码为:document.cookie="name="+username;JS读取cookie:var username=document.cookie.split(";")[0].split("=")[1];function setCoo...

js中如何往cookie中放值和取值(图文教程)

这个可以在网上下载下来,最基本的用法也很简单第一步:引入js 第二步:存放值$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });一步写到位,不要轻易把path去掉。不然只能在当前js使用,我吃...

js 如何读取cookie?

1、获取所有cookie function getCookies() { var showAllCookie = ''; if(!document.cookie == ''){ var arrCookie = document.cookie.split('; '); //用spilt('; ')切割所有cookie保存在数组arrCookie...

如何使用js来获取cookie的值

cookie.length; cookieValue = unescape (document.cookie.substring (offset, end)) } } return cookieValue; } function writeCookie (name, value, hours) { var expire = ""...

静态页面怎么用js获取cookie

如何使用js来获取cookie的值,读取属于当前文档的所有cookies,参考如下:var allcookies = document.cookie;定义一个函数,用来读取特定的cookie值。function getCookie(cookie_name){ var allcookies = document.cookie;var ...

JS 操作 cookies 的方法

在 JS(JavaScript) 操作cookies比较复杂,在 ASP 里面我们只需要知道 cookie 的名称、cookie 的值就行了,而 JS 里面,我们面对的是 cookie 的字符串,你自己编写这个字符串写入客户端,然后自己解析这个字符串。一、从写 ...

js使用cookie数组传值

下面是代码示例//赋值给cookiefunction setcookies(name,value){ var days = 30;//此 cookie 将被保存 30 天 var exp = new date();//new date("december 31,9998");exp.settime(exp.gettime()+ days*24*60*...

js代码 cookie操作 读取写入跳转

10); //设置cookie location.href = "h.html"; //跳转到指定链接 }} //设置cookie function setCookie(name, value, expiredays) { var ExpireDate = new Date(); ExpireDate.setTime(ExpireDate.get...

Top