/    Sign up×
Community /Pin to ProfileBookmark

how to get data from different cookies

how do i get back the data using javascript from different cookies that have value.

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@PalrosMar 31.2005 — you have to look for both cookies in document.cookie.

[CODE]
var names = [];
var data = new Array(names.length);
var cookies = document.cookie.split(";");
for (var i=0; i<cookies.length; i++)
{
for (var j=0; j<names.length; j++)
{
if (cookies[i].indexOf(names[j]+"=") != -1)
{
data[j] = cookies[i].substring(cookies[i].indexOf(names[j])+names[j].length+1,cookies[i].length);
}
}
}
[/CODE]


names is an array of the names of the cookies you want. data is the array which holds the data from the cookies at the end, in the same order as the names array. You will need to put in the names of all the cookies you are looking for, without the = sign. This assumes that the cookies are in this format:

name=escapeddata;(optional extra stuff like expires and path and domain with semicolons inbetween and at end)
Copy linkTweet thisAlerts:
@andrewchaiauthorMar 31.2005 — Thanks Palros !

i test it now.
Copy linkTweet thisAlerts:
@Warren86Mar 31.2005 — <HTML>

<Head>

<Script Language=JavaScript>

var expDate = new Date;
expDate.setTime(expDate.getTime()+365*24*60*60*1000); // one year

function setCookie(isName,isValue,dExpires){

document.cookie = isName+"="+isValue+";expires="+dExpires.toGMTString();
}

function getCookie(isName){

cookieStr = document.cookie;
startSlice = cookieStr.indexOf(isName+"=");
if (startSlice == -1){return null}
else
{
endSlice = cookieStr.indexOf(";",startSlice+1)
if (endSlice == -1){endSlice = cookieStr.length}
isData = cookieStr.substring(startSlice,endSlice)
isValue = isData.substring(isData.indexOf("=")+1,isData.length);
return isValue;
}
}

function dispCookie(isName){

isValue = getCookie(isName);
alert(isValue);
}

function deleteCookie(isName){

if (getCookie(isName)){document.cookie = isName + "="+
"; expires=Thu, 01-Jan-70 00:00:01 GMT";}
}


</Script>

</Head>

<Body>

<input type=button value='Set 1st Cookie' onclick="setCookie('anyName','hello',expDate)">

&nbsp;<input type=button value='Set 2nd Cookie' onclick="setCookie('someName','goodbye',expDate)">

<br><br>

<input type=button value='Delete 1st Cookie' onclick="deleteCookie('anyName')">

&nbsp;<input type=button value='Delete 2nd Cookie' onclick="deleteCookie('someName')">

<br><br>

<input type=button value='Read 1st Cookie' onclick="dispCookie('anyName')">

&nbsp;<input type=button value='Read 2nd Cookie' onclick="dispCookie('someName')">

</Body>

</HTML>
Copy linkTweet thisAlerts:
@PalrosMar 31.2005 — one little problem there, Warren...

you need to escape the value of a cookie before setting it. This allows you to use more characters in the cookie data.

so then change these to functions:

[CODE]
function setCookie(isName,isValue,dExpires){
document.cookie = isName+"="+escape(isValue)+";expires="+dExpires.toGMTString();
}

function getCookie(isName){
cookieStr = document.cookie;
startSlice = cookieStr.indexOf(isName+"=");
if (startSlice == -1)
return null;
else
{
endSlice = cookieStr.indexOf(";",startSlice+1)
if (endSlice == -1)
endSlice = cookieStr.length;
isData = cookieStr.substring(startSlice,endSlice)
isValue = isData.substring(isData.indexOf("=")+1,isData.length);
return unescape(isValue);
}
}
[/CODE]


and note that this:

if (test){statement;}

can also be written like this:

if(test)statment;

as long as there is only one statement in the block. The browser will automatically assume that if there are no curly braces, then the if() only applies to the first statement.
Copy linkTweet thisAlerts:
@Warren86Mar 31.2005 — Another one added to my ignore list...

All you posted was garbage, worthless garbage, and now you come along and stick your finger in my face: "change this, change that." My God, I am so sick and tired of you arrogant SOBs. Nothing better to do than to go around all day criticizing others.

Change your OWN code. And get your finger out of my face.
Copy linkTweet thisAlerts:
@PalrosMar 31.2005 — Sorry Warren. I was just trying to help.
Copy linkTweet thisAlerts:
@andrewchaiauthorApr 01.2005 — However i would like to thanks both of you.

sorry !!!!
×

Success!

Help @andrewchai spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...