Reply
Issues with readCookie()...
Old 08-22-2008, 02:24 AM Issues with readCookie()...
Skilled Talker

Posts: 70
Location: Atlanta, GA
Trades: 0
I'm trying to get my readCookie() function to read the variables set by SetCookie() and write them to the page using associative arrays. Any idea of how to do this? The code is below and I highlighted the areas of confusion in blue.

Code:
<html><head><title>Problem14</title>

<style type="text/css">
#greeting {position:absolute; left;50px; top:100px;}
</style>

<script type="text/javascript">


function readCookie(the_info)
{
  if(document.cookie)
  {
    var the_cookie = document.cookie;
    var the_cookie = unescape(the_cookie);

    var broken_cookie = the_cookie.split("=");
    
    var the_values = broken_cookie[1];
    var separated_values = the_values.split("/");

    
    var property_value="";//once you split the backslash you need loop through
                            // the values and store them in a variable so you
                               // can reference them later in the code

    for (var i=0; i<separated_values.length; i++)
    {
    var property_value=separated_values[i];

    var broken_info = property_value.split(":");
    var the_property = broken_info[0];
    var the_values = broken_info[1];
    the_info[the_property] = the_values;
    
    }
  }
}

function setCookie(){

//Set Up the Date String

var today = new Date();
var the_month = today.getMonth();
var the_day = today.getDate()+2;
var the_year = today.getFullYear();
var mth_list = new Array("January","February","March","April","May","June","July",
        "August","September","October","November","December");
var alpha_mth = mth_list[the_month];
var condensed_date = alpha_mth+" "+the_day+","+the_year;

//Set Up the Time String

var the_time = today.getTime();
var the_secs = today.getSeconds();
var the_time = Math.floor(the_time/60);
var the_mins = toSt(the_time%60);
var the_time = Math.floor(the_time/60);
var the_hours = toSt(the_time%24);
var the_time_string = the_hours+":"+the_mins+":"+the_secs;

//Set Up The Cookie

var the_cookie = "date:condensed_date/time:the_time_string";

//above...forcing it to read the variable..

document.cookie = "my_cookie="+escape(the_cookie);

//Call The Display Function

displayDateTime(condensed_date,the_time_string);

}


function toSt(n) {
var s=""
if (n<10) s+="0"
return s+n;
}

function displayDateTime(date,time){

var the_div=document.getElementById("greeting");
var the_message="Welcome. Today's date is "+date+" and the time is "+time;
var text=document.createTextNode(the_message);
the_div.appendChild(text);

}

var cookie_info = new Array();
readCookie(cookie_info);

</script></head>

<body onLoad=setCookie();>

<div id="greeting"></div>

<script type="text/javascript">


var the_name=prompt("what's your name","");
alert ("Welcome, "+the_name+" to my page.");


document.write("The last time you were here or the page was updated was on"+
               cookie_info["date"]+" at "+ cookie_info["time"]);
</script>



</body>
</html>
LayneMitch is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 08-25-2008, 08:45 PM Re: Issues with readCookie()...
Extreme Talker

Posts: 238
Location: United States
Trades: 0
I can't tell exactly what you are doing, but I'll start from the beginning for reading cookies.

document.cookie should contain a list of cookies separated by semi-colons. Each cookie should be a name=value pair. So alert(document.cookie) would give something like cookie1=value1;cookie2=value2;cookie3=value3 and so on.

So in order to read a value cookies, it's a good idea to split the document.cookie string into pieces. Then you can iterate though the list of cookies and split them down into their name and value pairs. You should not unescape the cookie value until after you have split it up-- otherwise, you could run into problems if your cookie value contains a semi-colon or an equals sign.

Here is a function which gets all of the cookies and returns them in a nice associative array:

Code:
function getCookies(){
    var myCookies = {};
    var cookies = document.cookie.split(';');
    
    for (var i = 0; i < cookies.length; i++) {
        var crumbs = cookies[i].split('=');
        var name = unescape(crumbs[0]).replace(/^\s+|\s+$/g, ''); // trim any whitespace
        var value = unescape(crumbs[1]).replace(/^\s+|\s+$/g, ''); // trim any whitespace
        
        myCookies[name] = value;
    }
    
    return myCookies;
}

// example usage to read a cookie:
var cookies = getCookies();
alert(cookies['my_cookie_name']);
__________________
The interlocking pieces of web development: usability, performance, accessibility, and standards.
frost is offline
Reply With Quote
View Public Profile
 
Old 08-27-2008, 09:20 AM Re: Issues with readCookie()...
Skilled Talker

Posts: 70
Location: Atlanta, GA
Trades: 0
Frost,

The problem has already been solved by now, but I just read through this and it does help. The advice about splitting before I unescape is new to me just like the JavaScript language. Also, the regular expression is very valuable as well. Thanks.
LayneMitch is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Issues with readCookie()...
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.10931 seconds with 13 queries