有2层结构的cookies 用js怎么写

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了有2层结构的cookies 用js怎么写相关的知识,希望对你有一定的参考价值。

有2层结构的cookies 用js怎么写
用C#是这样写的
HttpCookie cookie = new HttpCookie("dnt");
cookie.Values["Olid"] = "11";
cookie.Values["Userid"] = "22";
HttpContext.Current.Response.Cookies.Add(cookie);
请问要用js怎么写???
我需要的是能够灵活设置获取其中的某个键值 而不是一次性写入 因为这部分cookies 是要与c#同时操作的
不能这边用js改了cookies的一个值 却把c#写的其它的值都给改没了 不知道我说清楚没有
最好能举个例子

/**
*jQuery. Cookies plugin
*product by wuqingsssss
* 使用举例:
//写入Cookies-值为对象,则每个属性名为子键的名称,属性值为子键值
var subNameObj = UrlReferrer: "aaaUrlReferrer", UrlReferrer2: "bbbUrlReferrer", UrlReferrer3: "cccUrlReferrer" ;
$.Cookies.Set("ant", subNameObj, expires: 1, path: "/", secure: false );
//读取Cookies-根据主键和子键
alert($.Cookies.Get("ant"));
//获取HttpCookie对象型的数据对象
var aa=$.Cookies.HttpCookie("ant");
aa["UrlReferrer"]="22222";//为对象赋值
$.Cookies.Set("ant", aa);//是赋值生效并保存
//读取对象子键数据
alert(aa["UrlReferrer"]);//方法一
alert($.Cookies.Get("ant","UrlReferrer"));//方法二
//写入Cookies-值为字符,则每个属性名为子键的名称,属性值为子键值
$.Cookies.Set("UrlReferrer", "111111111", expires: 1, path: "/", secure: false );
//读取Cookies-根据主键
alert($.Cookies.Get("UrlReferrer"));
*
*/
jQuery.Cookies=new Object();
jQuery.Cookies.HttpCookie=function(name)
var cookieValue=jQuery.Cookies.Get(name);
if(cookieValue.indexOf("="))
eval("var cookiesub= "+(cookieValue.replace(/=/g,":'")).replace(/&/g,"',")+"'"+";");
return cookiesub;

else

return cookieValue;


jQuery.Cookies.Set = function(name, value, options)

options = options || ;
if (value === null)

value = '';
options.expires = -1;

var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString))

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.toUTCString(); // use expires attribute, max-age is not supported by IE

// CAUTION: Needed to parenthesize options.path and options.domain
// in the following expressions, otherwise they evaluate to undefined
// in the packed version for some reason...
var path = options.path ? '; path=' + (options.path) : ';path=/';
var domain = options.domain ? '; domain=' + (options.domain) : '';
var secure = options.secure ? '; secure' : '';

//If value is an object, each property will be a sub key;
if (typeof value == "object")

var k = 0;
var tempResult = "";
for (var tempValue in value)

if (k > 0)

tempResult += "&";

tempResult += tempValue + "=" + encodeURIComponent(value[tempValue]);
k++;

value = tempResult;

else

value = encodeURIComponent(value);


document.cookie = [name, '=', value, expires, path, domain, secure].join('');
;

jQuery.Cookies.Get = function(name, subName)

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));
//Search sub key
if (typeof subName != 'undefined' && subName != null && subName != "")

eval("var cookiesub= "+(cookieValue.replace(/=/g,":'")).replace(/&/g,"',")+"'"+";");
cookieValue=cookiesub[subName];


break;




if(cookieValue==null)cookieValue="";
return cookieValue;
;
参考技术A var the_cookie="usr="+escape($("#usr").val())+"=pwd="+escape($("#pwd").val());
document.cookie=the_cookie;

$("#usr").val()是jquery下一个id叫usr的input文本框的value值

js怎么设置div层左边距 及与顶部边距

参考技术A js设置style属性时。将“-”去掉,“-”后的字母大写。
如:左边距 margin-left .用js 写就是 style.marginLeft='34px';

以上是关于有2层结构的cookies 用js怎么写的主要内容,如果未能解决你的问题,请参考以下文章

怎样使用jquery里的cookie

js控制网页弹窗次数

问题1: js 不能往非80端口写cookie? 问题2: 如果访问题地址是IP地址,80端口,是不是能写cookie

如何给cookies设置过期时间?

怎么用JS隐藏一个DIV层?

JS——Cookie