Click to See Complete Forum and Search --> : problem with reading cookies


mat106
09-07-2004, 11:22 AM
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:

<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
<a href="http://www.ucl.ac.uk" onClick="javascript:setcookie('http://www.ucl.ac.uk', '')">UCL</a><span class="date">
<script>readcookie('http://www.ucl.ac.uk')</script>
</span><br>
<a href="http://www.phys.ucl.ac.uk" onClick="javascript:setcookie('http://www.phys.ucl.ac.uk', '')">UCL Physics Web
Site</a><span class="date">
<script>readcookie('http://www.phys.ucl.ac.uk')</script>
</span><br>
<a href="http://www.google.com" onClick="javascript:setcookie('http://www.google.com', '')">Google.com</a><span class="date">
<script>readcookie('http://www.google.com')</script>


What seems to be the problem??? Thanks.

Exuro
09-07-2004, 12:18 PM
Your problem had to do with the fact that each of the cookie names after the first one had a leading whitespace. This would make it so the equality test you were doing would fail. Here's the solution I came up with:

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("=");
// The replace() method removes any whitespace from the beginning of the string
var cookiename = single_cookie[0].replace(/^\s*(.*)$/,"$1")
var cookievalue = single_cookie[1]
if (cookiename == name) {
document.write ("<br>" +unescape(cookievalue))
break;
}
}
}

Edit:
I thought I should not that I had added in a couple other things as well, such as the break in the for loop, and the unescape().

mat106
09-07-2004, 02:31 PM
Thanks Exuro. Will give that a try and see if i can get it to work.

(/^s*(.*)$/,"$1")

The syntax in the bracket is unfamiliar to me. Is it specific to PHP for removing the whitespace? Do i have to work in PHP for this to work?

Exuro
09-07-2004, 05:54 PM
Sorry for the confusion. The syntax in the parenthesis is called a Regular Expression. I actually didn't learn about them until a couple years ago, and I was utterly confused when I would see them on the forum, just as you are probably right now. You can read more about Regular Expression in JavaScript at this site:

http://www.webreference.com/js/column5/

As for your question about PHP, sorry again for the confusion. Many people who post on this forum, and other forums, utilize the [php] vB Code tag to post JavaScript and PHP code is because it does automatic syntax highlighting. If you see something posted on the JavaScript forum in a [php] block you can safely assume it is JavaScript unless the post specifies that it isn't.