Variable Scope and Cookie Issue

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
tpra21
Forum Newbie
Posts: 22
Joined: Wed Jul 20, 2005 6:20 pm

Variable Scope and Cookie Issue

Post by tpra21 »

I have a function that executes when I send a request to the server. Inside the function, I grab the value of a cookie and then pass that value to the server by attaching it to the URL in the request. Unfortunately, I cannot successfully get the value of the cookie into the cookieValue variable. The first group of statements below (in red) contain the correct cookie value in the variable cookeValue; general. But, if I comment that line out and use the second group of red lines, the variable cookieValue has the following incorrect value; PHPSESSID=ad2307d8491584317b165ba0907a894f .

I am fairly new to using javascript and cookies, but from what I can see I have made the cookieValue variable global to the function, so I am completely confused on why this is happening.

Thanks in advance for any time spent and help - Adam


 
 

Code: Select all

  function sendRequest(){
 
        var cookie_name = "quick_view";
        var cookieValue;
        var namestart;
        var nameend;
        var index;
 
        if(document.cookie)
        {
            index = document.cookie.indexOf(cookie_name);
            if (index != -1)
            {
                namestart = (document.cookie.indexOf("=", cookie_name) + 1);
                nameend = document.cookie.indexOf(";", cookie_name);
                if (nameend == -1) {nameend = document.cookie.length;}
[color=#FF0000]                     cookieValue = document.cookie.substring(namestart, nameend);
                     //document.write(cookieValue);[/color]
            }
        }
                                 
[color=#FF0000]         //cookieValue = document.cookie.substring(namestart, nameend);
         //document.write(cookieValue);[/color]
 
        var mytime= "ms=" + new Date().getTime(); // use time to prevent IE from caching page
        var toggle= "&toggle=" + cookieValue;
        var url = "refreshHeader.php?" + escape(mytime) + escape(toggle);
 
        ajaxRequest.open("GET",url,true);
        ajaxRequest.onreadystatechange = updatePage;
        ajaxRequest.send(null);
    }
 
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Variable Scope and Cookie Issue

Post by kaszu »

You had 'cookie_name' instead of 'index' as second argument for indexOf. Correct:

Code: Select all

 
if(document.cookie)
{
    index = document.cookie.indexOf(cookie_name);
    if (index != -1)
    {
        namestart = (document.cookie.indexOf("=", index) + 1);
        nameend = document.cookie.indexOf(";", index);
        if (nameend == -1) {nameend = document.cookie.length;}
            cookieValue = document.cookie.substring(namestart, nameend);
            //document.write(cookieValue);
        }
    }
}
Good resource about javascript and cookies
tpra21
Forum Newbie
Posts: 22
Joined: Wed Jul 20, 2005 6:20 pm

Re: Variable Scope and Cookie Issue

Post by tpra21 »

Thanks for the reply. I fixed the two lines that you mentioned, but now the cookieValue field has "undefined" in it when accessing it outside the (if document.cookie) statement. It is if it has never been assigned a value, yet the cookieValue variable correctly holds the cookie value if I am accessing it within the if structure it exists in.

Any ideas?

Code: Select all

 
    function sendRequest(){
 
        var cookie_name = "quick_view";
        var cookieValue;
        var namestart;
        var nameend;
        var index;
 
        if(document.cookie)
        {
            index = document.cookie.indexOf(cookie_name);
            if (index != -1)
            {
                namestart = (document.cookie.indexOf("=", index) + 1);
                nameend = document.cookie.indexOf(";", index);
                if (nameend == -1) {nameend = document.cookie.length;}
                cookieValue = document.cookie.substring(namestart, nameend);
                //document.write(cookieValue);[color=#FF0000] [b]here the cookieValue variable has the correct cookie value[/b][/color]
            }
        }
 
        document.write(cookieValue); // [color=#FF0000][b]here the cookieValue variable is showing "undefined" even though it gets set above[/b][/color]
 
        var mytime= "ms=" + new Date().getTime(); // use time to prevent IE from caching page
        var toggle= "&toggle=" + cookieValue;
        var url = "refreshHeader.php?" + escape(mytime) + escape(toggle);
 
        ajaxRequest.open("GET",url,true);
        ajaxRequest.onreadystatechange = updatePage;
        ajaxRequest.send(null);
    }
 
tpra21
Forum Newbie
Posts: 22
Joined: Wed Jul 20, 2005 6:20 pm

Re: Variable Scope and Cookie Issue

Post by tpra21 »

I have figured it out. I was not setting an expiration date for the cookie, so it was expiring by the time my code was executed.
Post Reply