PDA

View Full Version : cookies - trouble reading them back



tpeck
Apr 2nd, 2007, 04:58 AM
Cookies - I hate them. And I'm still fat.

OK - I've set two cookies like this:

document.cookie="yourdrive="+driveletter+"; expires="+"; path=/";
document.cookie="maindrive="+menuletter+"; expires="+"; path=/";

They end up looking like this in the cookie:

yourdrive E ~~local~~/ 1088 647360864 29848791 647360864 29848791 * maindrive C ~~local~~/ 1088 647360864 29848791 647360864 29848791 *

So they must be being set.

I must have tried a dozen cookie reading routines and I can't read either of them back!

Can anyone do it? I am sure it must be simple. But all I get is "false" all the time.

This is basically what I have been using:



<script>
<!--
function getCookie() {
var driveletter=false;
nCookie=document.cookie.split(";");
for(i in nCookie){
if(nCookie[i].indexOf("yourdrive")!==-1){
var driveletter=nCookie[i].substring(nCookie[i].indexOf("=")+1,nCookie[i].length);
}}
alert(driveletter);
}
//-->
</script>
</head>

<body onload="getCookie();"

rwedge
Apr 2nd, 2007, 06:48 AM
Here's an example of setting and reading your cookie names in a page.
<script type="text/javascript">
/*<![CDATA[*/
var now=new Date();
var exp=new Date(now.getTime()+365*24*60*60*1000);
function getCookie(cookname) {
var cook=document.cookie.split("; ");
for (var i=0; i<cook.length; i++) {
chip=cook[i].split("=");
if (chip[0] == cookname) {
return unescape(chip[1]);
}
}
return "";
}
function setCookie(cookname, cookval) {
if (cookval != null && cookval != "") {
document.cookie=cookname+"="+escape(cookval)+"; expires="+exp.toGMTString();
}
}
function loadCookie(){
document.f.dl.value=getCookie('yourdrive');
document.f.ml.value=getCookie('maindrive');
}
window.onload=loadCookie;
/*]]>*/
</script>

<form name="f" id="f" method="post" action="" enctype="multipart/form-data">
Your Drive <input type="text" name="dl" id="dl" value="" size="3" maxlength="50" onchange="setCookie('yourdrive',this.value)" /><br />
Main Drive <input type="text" name="ml" id="ml" value="" size="3" maxlength="50" onchange="setCookie('maindrive',this.value)" />
</form>

tpeck
Apr 2nd, 2007, 04:53 PM
Thank you.

I can get your example to work but I am still having difficulty applying it my particular situation.

So far I can set the cookie like you showed me:

document.cookie="yourdrive="+(driveletter)+"; expires="+exp.toGMTString();

I would prefer it not to have an expiry date, though. I just want it to be there forever.

Here is how I am attempting to read the value "driveletter" back (it doesn't work yet).



<script>
function getCookie(cookname) {
var cook=document.cookie.split("; ");
for (var i=0; i<cook.length; i++) {
chip=cook[i].split("=");
if (chip[0] == cookname) {
return unescape(chip[1]);
}
}
return "";
}

function loadCookie(){
document.driveletter.value=getCookie('yourdrive');
}
</script>
</head>

<body onload="loadCookie();" scroll="no" bgcolor="#000000">
<div align="center">
<center>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
<tr>
<td width="100%">
<p align="center"><span style="cursor: hand"><map name="FPMap0">
<area href="#" onclick="window.navigate('file:///'+driveletter+':/IndexSetup.exe')" shape="rect" coords="13, 320, 167, 340">
<area href="#" onclick="self.location.href=('how2close.html');" shape="rect" coords="203, 320, 341, 340">
</map></span><img border="0" src="../img/loadindex.jpg" width="342" height="340" usemap="#FPMap0"><br>
</p>
</td>
</tr>
</table>



The blue bit is where I need to use it. I think that red bit's wrong.

Is it easily fixable?

rwedge
Apr 2nd, 2007, 07:33 PM
Change
function loadCookie(){
document.driveletter.value=getCookie('yourdrive');
} to

driveletter=getCookie('yourdrive');
to get the driveletter value before the page renders and the map code is written.
You will not need the onload event handler in the body tag.

To see when the cookie is set to expire:
<script type="text/javascript">
/*<![CDATA[*/
var now=new Date();
var exp=new Date(now.getTime()+365*24*60*60*1000);
alert(exp);
/*]]>*/
</script>

tpeck
Apr 3rd, 2007, 02:02 AM
Yes, that did it. And works well. Thank you very much.

I am assuming that I get 10 years if I add:

var exp=new Date(now.getTime()+10*365*24*60*60*1000);

...but what if I want it to never expire?

I tried this but it didn't work:

.................expires="+"; path=/";


The path bit I don't think I need - at least, your example works without it.

rwedge
Apr 3rd, 2007, 03:20 AM
The default is path=/ , you can use path=/dir_name

document.cookie=cookname+"="+escape(cookval)+"; path=/; expires="+exp.toGMTString();
(days x hours x minutes x seconds x 1000) gives you the days value in milliseconds
so, 36500 * 24 * 60 * 60 * 1000 is equal to 3153600000000, 100 years not accounting for leap years.
Realistically, setting the expiration date a few years into the future should outlast most OS installitions

tpeck
Apr 3rd, 2007, 03:45 AM
100 years is great.

Sorry to keep troubling you, but it all works now EXCEPT that I don't really want to put all that getCookie code on every page that references the function.

So I put it in an external .js:


function getCookie(cookname) {
var cook=document.cookie.split("; ");
for (var i=0; i<cook.length; i++) {
chip=cook[i].split("=");
if (chip[0] == cookname) {
return unescape(chip[1]);
}
}
return "";
}

driveletter=getCookie('yourdrive');



...just like that. Then I reference it as you do with any external .js:

<script src="../js/getcookievalue.js" language="JavaScript1.2"></script>

But it doesn't seem to read the value and return it. It flops.

Why is that? Do I really have to put it on every page (all 1200 of them?)

rwedge
Apr 3rd, 2007, 04:47 AM
As long as the cookie is set, driveletter should hold the cookie value when the external js runs as it loads.
You can then use the cookie value in your form by sticking this script below or after the form

<script type="text/javascript">
document.formName.elementName.value=driveletter;
</script>

tpeck
Apr 3rd, 2007, 05:53 AM
I see.

The problem is it is not part of a form.

How can I name the <area href....... >

...and correctly make your line below work?


<script type="text/javascript">
document.formName???.elementName.value=driveletter;
</script>


<p align="center"><span style="cursor: hand"><map name="FPMap0">
<area href="#" onclick="window.navigate('file:///'+driveletter+':/IndexSetup.exe')" shape="rect" coords="13, 320, 167, 340">
<area href="#" onclick="self.location.href=('how2close.html');" shape="rect" coords="203, 320, 341, 340">
</map></span><img border="0" src="../img/loadindex.jpg" width="342" height="340" usemap="#FPMap0"><br>
</p>


I have to get this right so I can repeat it 1000+ times!

rwedge
Apr 3rd, 2007, 07:20 AM
One way to add the value to the map link would be to write the map code on the fly from the external js file

getcookievalue.js

function getCookie(cookname) {
var cook=document.cookie.split("; ");
for (var i=0; i<cook.length; i++) {
chip=cook[i].split("=");
if (chip[0] == cookname) {
return unescape(chip[1]);
}
}
return "";
}
var driveletter=getCookie('yourdrive');
function myMap() {
var mapHTML='<map name="FPMap0"><area href="#" onclick="window.navigate(\'file:///'+driveletter+':/IndexSetup.exe\')" shape="rect" coords="13, 320, 167, 340">';
mapHTML+='<area href="#" onclick="self.location.href=(\'how2close.html\');" shape="rect" coords="203, 320, 341, 340"></map>';
document.body.innerHTML+=mapHTML;
}
window.onload=myMap;

in html page



<script src="../js/getcookievalue.js" type="text/javascript"></script>

<p align="center"><img border="0" src="../img/loadindex.jpg" width="342" height="340" usemap="#FPMap0"></p>

tpeck
Apr 3rd, 2007, 07:34 AM
That is useful to know and I can certainly see where I can use that in the future; but in this case, the way in which the "driveletter" variable is used on the hundreds of pages varies immensely.

So I suppose the only thing to do is repeat the "getcookie" function on every page that needs the variable. It just seems so inelegant.

I just feel I ought to be able to delve into the cookie, grab the single letter by referencing a single external .js that will work for "driveletter" wherever it is required and however it is implemented on the page.

But maybe not.

tpeck
Apr 3rd, 2007, 04:22 PM
Update.

It does work. The problem I was having was not using ; path=/;

That made it impossible to read from another page in another folder. Why would anyone invent such a restriction?

Just hate these cookies!

Anyhow - many thanks. I can continue now.