Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with reading cookies

Status
Not open for further replies.

mat106

Programmer
Sep 7, 2004
3
GB
Hi. I have a page with many links to external sites on it and i want to use cookies to display the last time each link was visited next to the link. I think the script i have for setting the cookie is ok: i'm using firefox and the various cookies appear under the cookies options. However instead of displaying only the relevant date next to each link it displays the dates for all of the links. It is as if it is completely ignoring the if statement that i have placed in bold in the script below:

This is in the head:

Code:
<script type="text/javascript">
function setcookie(name, date) {
newdate = new Date();
day = newdate.getDate();
month = newdate.getMonth () + 1;
year = newdate.getYear ();
hours = newdate.getHours ();
minutes = newdate.getMinutes ();
seconds = newdate.getSeconds ();
date = day+"/"+month+"/"+year+""+hours+"-"+minutes+"-"+seconds;
expirydate = new Date("December 31, 2023");
cookieexpirydate = expirydate.toGMTString();
document.cookie = name + "=" + escape(date) + ";expires=" + cookieexpirydate;
<!-- location.href = name; -->
}

function readcookie (name) 
{
var cookie_string = document.cookie;
var cookie_array = cookie_string.split(";");
for (i=0; i <= cookie_array.length; i++)
	{
	var single_cookie = cookie_array[i].split("=")
	var cookiename = single_cookie[0]
	var cookievalue = single_cookie[1]
		if (cookiename = name) document.write ("<br>" +cookievalue)
	}
}</script>
and this is in the body
Code:
<a href="[URL unfurl="true"]http://www.ucl.ac.uk"[/URL] onClick="javascript:setcookie('[URL unfurl="true"]http://www.ucl.ac.uk',[/URL] '')">UCL</a><span class="date"> 
<script>readcookie('[URL unfurl="true"]http://www.ucl.ac.uk')</script>[/URL] 
</span><br> 
<a href="[URL unfurl="true"]http://www.phys.ucl.ac.uk"[/URL] onClick="javascript:setcookie('[URL unfurl="true"]http://www.phys.ucl.ac.uk',[/URL] '')">UCL Physics Web
Site</a><span class="date"> 
<script>readcookie('[URL unfurl="true"]http://www.phys.ucl.ac.uk')</script>[/URL] 
</span><br>
<a href="[URL unfurl="true"]http://www.google.com"[/URL] onClick="javascript:setcookie('[URL unfurl="true"]http://www.google.com',[/URL] '')">Google.com</a><span class="date"> 
<script>readcookie('[URL unfurl="true"]http://www.google.com')</script>[/URL]

What seems to be the problem??? Thanks.
 
I don't handle cookies much (by which I mean 'not at all'), but if you cut-and-paste your code as is, the mistake is here:

Code:
if (cookiename = name) document.write ("<br>" +cookievalue)

...should be...

Code:
if (cookiename [b]==[/b] name) document.write ("<br>" +cookievalue)

[notice the double-equals sign]

'hope that helps.

--Dave
 
Thanks. That was an error in the script but it doesn't seem to solve the problem. The effect its had is that the date is now only displayed next to the last clicked link and not next to any of the other links. I can't figure out why since the readcookie() script is executed for each of the three links.
 
This is because you reset the cookie value whenever someone clicks one of those links. In the setcookie(...) function, you have:

Code:
document.cookie = name + "=" + escape(date) + ";expires=" + cookieexpirydate;

In place of this line, you might consider something like (adopted from your own readcookie(...) function):

Code:
var cookie_string = document.cookie;
var cookie_array = cookie_string.split(";");
var newcookie = "";

for (i=0; i < cookie_array.length; i++) [b]//not [i]<=[/i], by the way[/b]
{
    var single_cookie = cookie_array[i].split("=");
    var cookiename = single_cookie[0];
    var cookievalue = single_cookie[1];
    if (cookiename == name)
     newcookie += name + "=" + escape(date) + ";"; [b]//note the use of [i]+=[/i][/b]
    else if(cookiename == "expires")
     newcookie += expires=" + cookieexpirydate + ";";
    else
     newcookie += cookie_array[i] + ";";
}

newcookie = newcookie.substring(0, newcookie.length-1); [b]//removes the final semi-colon[/b]

document.cookie = newcookie;

'hope that helps.

--Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top