admin管理员组文章数量:1335386
Hi I have a small problem. Currently I have 2 parameters saved in the browser cookie which are ADV and LOC... Now I have a page with a form and the form got two hidden fields :
<input type="hidden" name="adv" value="" />
<input type="hidden" name="loc" value="" />
I need to get the values of adv and loc from the cookie and save them in the hidden form fields... How can i do this please? Thanks
Hi I have a small problem. Currently I have 2 parameters saved in the browser cookie which are ADV and LOC... Now I have a page with a form and the form got two hidden fields :
<input type="hidden" name="adv" value="" />
<input type="hidden" name="loc" value="" />
I need to get the values of adv and loc from the cookie and save them in the hidden form fields... How can i do this please? Thanks
Share Improve this question asked Feb 17, 2011 at 16:39 Rene ZammitRene Zammit 1,1515 gold badges19 silver badges42 bronze badges 1- possible duplicate of Any simplest way to get cookie value in javascript – Felix Kling Commented Feb 17, 2011 at 16:43
2 Answers
Reset to default 5document.cookie
will get you all the cookies in the following format:
'adv=adv_val; loc=loc_val;'
To get a value from a cookie, you can use this function (from quirksmode):
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
To fill in the hidden fields, you can loop though all the hidden fields, and get their respective cookies:
function hiddenCookies(){
var inputs = document.getElementsByTagName('input');
for(var i = 0; i < inputs.length; i++){
var element = inputs[i];
if(element.getAttribute('type') == 'hidden'){
element.value = readCookie(element.name);
}
}
}
And then modify <body>
to have an onload
.
<body onload="hiddenCookies()">
Or with jQuery:
$(function(){
$('input:hidden').each(function(i,v){
v.value = readCookie(v.name);
});
});
You can use this snippet to convert the cookie string into a map.
let cookieMap = document.cookie.split(";").map((str)=>str.split("=")).reduce(reduceToMap(map,curr),{});
function reduceToMap(map,currentArray) {
// convert array to map
map[currArray[0]] = currentArray[1];
return map;
}
What this is doing is essentially the following:
- Splitting the string into an array of strings (of the form "key=value")
- Mapping each string of this array to an array of the form ["key","value"]
- Reducing this array of arrays (of the form [ ["k1","v1"], ["k2","v2"] ]) into a map
Now you can simply access any cookie by doing the following:
let cookieName = "name of the cookie you want to access";
let cookieValue = cookieMap[cookieName];
本文标签: javascriptget parameter value from cookieStack Overflow
版权声明:本文标题:javascript - get parameter value from cookie - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742389402a2465687.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论