Javascript如何直接读取cookies子键值?
2012-12-03 17:24
302 查看
需要在服务器端创建多值cookie,类似:
HttpCookie aCookie = new HttpCookie(“userInfo”);
aCookie.Values["userName"] = admin”;
aCookie.Values["lastVisit"] = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);
在前端aspx中通过js直接读取userInfo的某个值,比如:getCookie(“userName”),应该怎么来实现?
除了先在服务器端Request后,再在js中通过<%=%>访问之外,类似下面,有没第二种方法,google后也没有更好的方法!
.cs中
HttpCookie aCookie = Request.Cookies["userInfo"];
string userName=aCookie.Values[userName];
string lastVisit=aCookie.Values[lastVisit];
js文件中:
var userName=<%=userName%>
答案:先读到主键,然后split,如下详细的代码:
JScript code:
function getCookie(name) {
var arr = document.cookie.match(new RegExp(“(^| )userInfo=([^;]*)(;|$)”));
if (arr != null) {
arr = unescape(arr[2]).split(“&”);
for (x in arr)
if (arr[x].split(“=”)[0] == name) {
return arr[x].split(“=”)[1];
}
}
else {
return “”;
}
}
alert( getCookie(“userName”))
JScript code:
function getCookie(key, name) {
var arr = document.cookie.match(new RegExp(“(^| )” + key + “=([^;]*)(;|$)”));
if (arr != null) {
arr = unescape(arr[2]).split(“&”);
for (x in arr) {
if (arr[x].split(“=”)[0] == name) {
return arr[x].split(“=”)[1];
}
}
}
return “”;
}
alert(getCookie(“userInfo”, “userName”))
alert(getCookie(“userInfo”, “lastVisit”))
此文由Web开发之答疑解惑源整理,若需转载,请注明原文(Javascript如何直接读取cookies子键值?)出处:http://www.znjcx.com/html/y2012/1634_javascript-how-to-read-cookies-child-key-values-directly.html,谢谢!
更多热门文章:
1.如何实现点击获取已取得的值?
2.如何用js代码实现验证输入框为空?
3.解决js在google,firfox下出现的bug
4.关于Repeater绑定数据中关键字高亮的问题
5.如何实现将创建的多个DIV用按钮关闭?
HttpCookie aCookie = new HttpCookie(“userInfo”);
aCookie.Values["userName"] = admin”;
aCookie.Values["lastVisit"] = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);
在前端aspx中通过js直接读取userInfo的某个值,比如:getCookie(“userName”),应该怎么来实现?
除了先在服务器端Request后,再在js中通过<%=%>访问之外,类似下面,有没第二种方法,google后也没有更好的方法!
.cs中
HttpCookie aCookie = Request.Cookies["userInfo"];
string userName=aCookie.Values[userName];
string lastVisit=aCookie.Values[lastVisit];
js文件中:
var userName=<%=userName%>
答案:先读到主键,然后split,如下详细的代码:
JScript code:
function getCookie(name) {
var arr = document.cookie.match(new RegExp(“(^| )userInfo=([^;]*)(;|$)”));
if (arr != null) {
arr = unescape(arr[2]).split(“&”);
for (x in arr)
if (arr[x].split(“=”)[0] == name) {
return arr[x].split(“=”)[1];
}
}
else {
return “”;
}
}
alert( getCookie(“userName”))
JScript code:
function getCookie(key, name) {
var arr = document.cookie.match(new RegExp(“(^| )” + key + “=([^;]*)(;|$)”));
if (arr != null) {
arr = unescape(arr[2]).split(“&”);
for (x in arr) {
if (arr[x].split(“=”)[0] == name) {
return arr[x].split(“=”)[1];
}
}
}
return “”;
}
alert(getCookie(“userInfo”, “userName”))
alert(getCookie(“userInfo”, “lastVisit”))
此文由Web开发之答疑解惑源整理,若需转载,请注明原文(Javascript如何直接读取cookies子键值?)出处:http://www.znjcx.com/html/y2012/1634_javascript-how-to-read-cookies-child-key-values-directly.html,谢谢!
更多热门文章:
1.如何实现点击获取已取得的值?
2.如何用js代码实现验证输入框为空?
3.解决js在google,firfox下出现的bug
4.关于Repeater绑定数据中关键字高亮的问题
5.如何实现将创建的多个DIV用按钮关闭?
相关文章推荐
- 如何利用Pro*C直接读取dbf文件
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- xml中直接读取properties键值的解决方式
- arcgis api for javascript从地图如何读取要素
- javascript如何读取Json数据结构节点的值?
- MongoDB server side Javascript 如何直接传入字符串?
- Metro中如何直接读取图片并显示出来。
- PHP如何读取由JavaScript设置的Cookie
- javascript 如何 读取dropdownlist 的值 (转)
- 装饰者模式的学习(c#) EF SaveChanges() 报错(转载) C# 四舍五入 保留两位小数(转载) DataGridView样式生成器使用说明 MSSQL如何将查询结果拼接成字符串 快递查询 C# 通过smtp直接发送邮件 C# 带参访问接口,WebClient方式 C# 发送手机短信 文件 日志 写入 与读取
- Nutch如何读取CrawlDb中的<Text,CrawlDatum>键值对
- 请问如何用javascript读取txt文本的值,再去空格后输出?
- javascript原生移动云编程9 - 如何使用云数据库读取和保存数据
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- 如何用VC++直接读取复合文档属性(如作者、公司、创建时间等)
- 如何在java web工程下建立存储property文件的文件夹,让Java程序直接读取
- 使用javascript以键值对的方式读取查询字符串【QueryString】
- 如何从javascript直接调用word插件
- 如何读取Linux键值,输入子系统,key,dev/input/event,dev/event,C语言键盘
- 你如何让Javascript和c直接互调?