how can i get all the cookies set by my site using js. I dont want to do say Cookie("username") but loop through all the cookies and get the key=value pairs of my site
how can i get all the cookies set by my site using js. I dont want to do say Cookie("username") but loop through all the cookies and get the key=value pairs of my site
Share Improve this question edited Feb 8, 2011 at 5:28 aWebDeveloper asked Feb 7, 2011 at 9:55 aWebDeveloperaWebDeveloper 38.4k41 gold badges177 silver badges247 bronze badges 1-
You've tagged this PHP and JavaScript. Which language are you using? Neither of them have a built in function called
getCookie
. – Quentin Commented Feb 7, 2011 at 9:58
4 Answers
Reset to default 4var cookies = document.cookie.split(/;/);
for (var i = 0, len = cookies.length; i < len; i++) {
var cookie = cookies[i].split(/=/);
alert("key: " + cookie[0] + ", value: " + cookie[1]);
}
You can use the getCookie
from my answer to Javascript getCookie functions and split it into a getCookies
and getCookie
function where the getCookies
function simply returns cookies
instead of cookies[name]
. And for the getCookie
function just take the return value of getCookies
and use [name]
on it.
Update Ok, I simply added the functions according to the description above. :)
Please read about reading/writing cookies in JavaScript.
http://www.quirksmode/js/cookies.html
You are probably looking for a solution like this: Get all cookies with Javascript
The following function loads all the cookie items into an associative array with the cookie name as the index and the cookie value as the value:
function get_cookies_array() {
var cookies = { };
if (document.cookie && document.cookie != '') {
var split = document.cookie.split(';');
for (var i = 0; i < split.length; i++) {
var name_value = split[i].split("=");
name_value[0] = name_value[0].replace(/^ /, '');
cookies[decodeURIComponent(name_value[0])] = decodeURIComponent(name_value[1]);
}
}
return cookies;
}
After that, you can get the cookies and write them out into the document like this:
var cookies = get_cookies_array();
for(var name in cookies) {
document.write( name + " : " + cookies[name] + "<br />" );
}
<html>
<head>
<script type="text/javascript">
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
}
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
function checkCookie()
{
var username=getCookie("username");
if (username!=null && username!="")
{
alert("Wele again " + username);
}
else
{
username=prompt("Please enter your name:","");
if (username!=null && username!="")
{
setCookie("username",username,365);
}
}
}
</script>
</head>
<body onload="checkCookie()">
</body>
</html>
copy pasted from : http://www.w3schools./JS/js_cookies.asp