Closed Thread Icon

Topic awaiting preservation: cookies Pages that link to <a href="https://ozoneasylum.com/backlink?for=8311" title="Pages that link to Topic awaiting preservation: cookies" rel="nofollow" >Topic awaiting preservation: cookies\

 
Author Thread
corrino
Bipolar (III) Inmate

From: Deep Space
Insane since: Dec 2000

posted posted 09-16-2002 18:11

I've been having some issue attempting to request a cookie through the use of JavaScript. Basically I'm taking a cookie value that I'm setting through the use of VBScript...then reading the value in a client side JS fuction to keep a DHTML effect I'm using the same from page to page. The issue that I'm having is not being able to read a cookie that has mulitple values.....
example)
<%
'Server side code in VBScript setting the cookie
Response.Cookies("userInfo")("menu1") = "closed"
Response.Cookies("userInfo")("menu2") = "open"
%>

The next bit of code is the JS code for reading the cookie, but I can't seem to get it to work for a cookie that has multiple data values...
<script LANGUAGE="JavaScript">
<!--//
function getCookie (name) {
var dcookie = document.cookie;
var cname = name + "=";
var clen = dcookie.length;
var cbegin = 0;
while (cbegin < clen) {
var vbegin = cbegin + cname.length;
if (dcookie.substring(cbegin, vbegin) == cname) {
var vend = dcookie.indexOf (";", vbegin);
if (vend == -1) vend = clen;
return unescape(dcookie.substring(vbegin, vend));
}
cbegin = dcookie.indexOf(" ", cbegin) + 1;
if (cbegin == 0) break;
}
return null;
}

//-->
</script>

If anyone could lend a little insight on this one I'd be very grateful.......

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 09-16-2002 19:49

write multiple cookies for each data value?

heheh


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

corrino
Bipolar (III) Inmate

From: Deep Space
Insane since: Dec 2000

posted posted 09-16-2002 21:33

That was something that I was considering doing that....Though I was prefering to keep it all in one cookie....does JS have a way to extract information from a cookie like this?


corr

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 09-16-2002 22:00

alert(document.cookie);

execute that line, and you can see where the information is. Then you have to get it out of the string using regular expressions or substring functions.

corrino
Bipolar (III) Inmate

From: Deep Space
Insane since: Dec 2000

posted posted 09-18-2002 18:06

Thanks for the help....I've got it working to where I can finally get each nested value. Here is what I have so far....it works.....thankfully. If you have any thoughts on condencing the code or comments....let me know......

corr

<%@ Language=VBScript %>
<%
'setting initial cookie value
Response.Cookies("userInfo")("side1") = "1"
Response.Cookies("userInfo")("side2") = "2"
Response.Cookies("userInfo")("side3") = "3"
%>
<HTML>
<HEAD>
<SCRIPT LANGUAGE="javaScript">
<!--//
function getCookie (name) {
var dcookie = document.cookie;
var cname = name + "=";
var clen = dcookie.length;
var cbegin = 0;
while (cbegin < clen) {
var vbegin = cbegin + cname.length;
// checks for a specific cookie through cname value
if (dcookie.substring(cbegin, vbegin) == cname) {
var vend = dcookie.indexOf (";", vbegin);
if (vend == -1) vend = clen;
//main cookie found
var RESULT_1 = dcookie.substring(vbegin, vend);
//main cookie -- separating subset cookies by "&" delimiter
var FINAL = RESULT_1.split("&");
for(i=0; i<3; i++){
//main cookie -- separating subset values from subset cookies by "=" delimeter
var temp = FINAL[i].split("=");
FINAL[i] = temp[1];
}
return FINAL
}
// if cookie name is not found on first try:
//code looks at next cookie name by checking where space is between cookies
cbegin = dcookie.indexOf(" ", cbegin) + 1;
if (cbegin == 0) break;
}
return null;
}


//-->
</SCRIPT>
</HEAD>
<BODY>

<SCRIPT LANGUAGE="javaScript">
<!--//
document.write(getCookie("userInfo"));
//-->
</SCRIPT>
</BODY>
</HTML>

« BackwardsOnwards »

Show Forum Drop Down Menu