PDA

View Full Version : Can't Get Cookies ...any ideas?



tedteeter
09-16-2013, 03:01 AM
Greetings, September 15th, 2013

Usually I use php to retrieve cookies, however, Javascript, was decided to be more convenient while the results were to be utilized by "Document.GetelementbyId" I have used the simple javascript getCookie" which didn't work! After some tinkering I elected to use php and pass the value to javascript but even my php cookie routine didn't return the cookie as it usually does?? I then resorted to an example at "W3schools" with the below! It passes fine an the alert reports "Null" I have checked and the cookie was written while there are others from my same domain and they retrieve fine?.......Additionally, I just attempted to see the value string of the cookies and it only listed a few of the cookies and not all....any ideas? Below is the latest attempt!

Appreciatively,


function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
alert(c_value);
return c_value;
}
}

jscheuer1
09-16-2013, 03:55 AM
There is no such thing as "Document.GetelementbyId". There is however a:

document.getElementById

Upper and lower case letters are almost always critical in javascript.

That said, the getCookie function in your post is serviceable. Except that it has an extra trailing } after it, which I've marked in red in your post. It should be:


function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
alert(c_value);
return c_value;
}



But there is a more efficient way:


function getCookie(n){ // getCookie takes (n) - the name of the cookie
var c = document.cookie.match('(^|;)\x20*' + n + '=([^;]*)');
return c? unescape(c[2]) : null;
}

tedteeter
09-16-2013, 05:10 AM
John, September 16th 2013 (early am)

Your solution worked great!...magnificent, actually I did locate a problem, the cookie must be written correctly at first, oddly, when I changed the "path" to "/" rather than a subdirectory! all worked along with your solution on all processes I attempted...your help is much appreciated!...Great work!

jscheuer1
09-16-2013, 07:28 AM
You can use this to set a cookie:


function setCookie(n, v, d){ // setCookie takes (name, value, optional_persist_days) - defaults to session only cookie if no days specified
if(d){var dt = new Date();
dt.setDate(dt.getDate() + d);
d = '; expires=' + dt.toGMTString();}
document.cookie = n + '=' + escape(v) + (d || '') + '; path=/';
}



You can set a folder as a part of the path. But if the syntax is wrong, it would be like setting no cookie at all. And of course, if set correctly, that cookie is only available to be read in that folder.

tedteeter
09-17-2013, 03:46 AM
John, September 16th 2013

That is a great function for the "setcookie"..I'll keep it handy!....I much appreciate your help :-) on a couple of occasions your help has been invaluable and saved me a tremendous amount of frustration...Great Work!
Appreciatively,
Ted