jquery怎么读取cookie里面每一个的值
使用下面代码$(document).ready(function () {
var MyCookie = $.cookie('userInfo');
alert(MyCookie);
});
最后得到的就是这个
我希望得到的是把每一个值都能单独读出来。
就像下面的JS就能实现的功能。
function GetCookie(sMainName, sSubName) {
var re = new RegExp((sSubName ? sMainName + "=(?:.*?&)*?" + sSubName + "=([^&;$]*)" : sMainName + "=([^;$]*)"), "i");
return re.test(unescape(document.cookie)) ? RegExp["$1"] : "";
}
alert(GetCookie("userInfo", "name"));
在就是如何用jquery删除cookie
这样删除$.cookie(’the_cookie’, null); 在不关闭网页之前,就算从新登陆,也不会记录COOKIE一直都是NULL,关闭打开网页后,之前的值依然存在。 jquery cookie .net --------------------编程问答-------------------- 补充一下,使用$.removeCookie('the_cookie');这种删除方法也不行,返回的是true,但是刷新页面cookie的值依然存在。 --------------------编程问答-------------------- 删除cookie,你设置cookie的有效期为当前时间减1秒就可以了
$.cookie(userInfo, "", { expires: -1, path: '/' }); --------------------编程问答-------------------- 少些了双引号
$.cookie("userInfo", "", { expires: -1, path: '/' }); --------------------编程问答-------------------- 读cookie,就是这样读的啊,你是不是存cookie的时候就把整个串保存进去的?
$.cookie('userInfo');//就是这样读 --------------------编程问答-------------------- 感谢楼上回答,我是在后台这样写的,代码如下
HttpCookie aCookie2 = new HttpCookie("userInfo");
aCookie2.Values["name"] = name;
aCookie2.Values["pwd"] = pwd;
aCookie2.Values["Utype"] = type;
aCookie2.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie2);
我想知道,怎么单独读出每一个值
就像下面这个JS,能单独读出每一个值
function GetCookie(sMainName, sSubName) {
var re = new RegExp((sSubName ? sMainName + "=(?:.*?&)*?" + sSubName + "=([^&;$]*)" : sMainName + "=([^;$]*)"), "i");
return re.test(unescape(document.cookie)) ? RegExp["$1"] : "";
} --------------------编程问答-------------------- 读取
function GetCookie(mname,sname)--------------------编程问答--------------------
{
var cookies= $.cookie(mname).split('&');
var res='';
for(var i=0;i<cookies.length;i++)
{
var one=cookies[i].split('=');
if(one[0]==sname)
{
res = one[1];
break;
}
}
return res ;
}
这样确实能够删除,谢谢。关于读取,不知道大虾有没有良策啊 --------------------编程问答--------------------
您这个貌似我用过但是IE不兼容,而且我想知道的是JQ的 --------------------编程问答-------------------- 怎么会不兼容,这不是标准的js吗?
非得用正则啊 --------------------编程问答-------------------- 则会个我也搞不明白,但是是火狐能够获取到值,IE就获取不到,然后就用的正则。
现在的关键是jquery,如何获取? --------------------编程问答--------------------
var cookie='<%=Request.Cookie["key"].value%>';纯手写的 .可能有问题. --------------------编程问答-------------------- lz处女铁,帮顶 --------------------编程问答--------------------
大哥,能写清楚用法吗?我智商佐剂啊 --------------------编程问答--------------------
Request.Cookies["key"].value 是获取某个键的cookie 的value啊 .
--------------------编程问答-------------------- 亲,目测你的是C#写法吧? --------------------编程问答--------------------
额 ,人才 .javascript中不能用么 大哥? --------------------编程问答-------------------- 既然你已经全部取出来了,分割一下不行么? --------------------编程问答-------------------- 既然你已经全部取出来了,分割一下不行么? --------------------编程问答-------------------- 哇,真是楼主处女贴唉 。
楼主,你既然取到了 cookies的所有值,用字符串分割 什么的各种办法都可以啊。 --------------------编程问答-------------------- 好吧,写写各位指导,我就是太懒了 --------------------编程问答-------------------- 其实楼主很你的想法实现很简单,你存cookie的时候 存两个cookie,每个cookie只有唯一的一个值,那么 alert(MyCookie) 就只弹出一个值 --------------------编程问答-------------------- 试试它:
/*!
* jQuery Cookie Plugin
* https://github.com/carhartl/jquery-cookie
*
* Copyright 2011, Klaus Hartl
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://www.opensource.org/licenses/mit-license.php
* http://www.opensource.org/licenses/GPL-2.0
*/
/*①、Create expiring cookie, 7 days from then:*/
//$.cookie('the_cookie', 'the_value', { expires: 7 });
/*②、Read cookie:*/
//$.cookie('the_cookie'); // => 'the_value' or Null
/*③、Delete cookie by passing null as value:*/
//$.cookie('the_cookie', null);
(function($) {
$.cookie = function(key, value, options) {
// key and at least value given, set cookie...
if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value === null || value === undefined)) {
options = $.extend({}, options);
if (value === null || value === undefined) {
options.expires = -1;
}
if (typeof options.expires === 'number') {
var days = options.expires, t = options.expires = new Date();
t.setDate(t.getDate() + days);
}
value = String(value);
return (document.cookie = [
encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
options.secure ? '; secure' : ''
].join(''));
}
// key and possibly options given, get cookie...
options = value || {};
var decode = options.raw ? function(s) { return s; } : decodeURIComponent;
var pairs = document.cookie.split('; ');
for (var i = 0, pair; pair = pairs[i] && pairs[i].split('='); i++) {
if (decode(pair[0]) === key) return decode(pair[1] || ''); // IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, thus pair[1] may be undefined
}
return null;
};
})(jQuery);
补充:.NET技术 , ASP.NET