Results 1 to 5 of 5

Thread: Can't Get Cookies ...any ideas?

  1. #1
    Join Date
    Jul 2013
    Posts
    41
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Can't Get Cookies ...any ideas?

    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,

    Code:
    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;
    }
    }
    Last edited by jscheuer1; 09-16-2013 at 03:56 AM. Reason: Format code

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    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:

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

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jul 2013
    Posts
    41
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    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!

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You can use this to set a cookie:

    Code:
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jul 2013
    Posts
    41
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    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

Similar Threads

  1. Replies: 16
    Last Post: 11-11-2009, 07:30 AM
  2. Cookies instead of session cookies?
    By DisturbedRoach in forum JavaScript
    Replies: 1
    Last Post: 10-21-2008, 09:12 PM
  3. Any ideas on How?
    By snic07 in forum PHP
    Replies: 2
    Last Post: 07-06-2007, 02:48 PM
  4. Any ideas?
    By alexjewell in forum PHP
    Replies: 1
    Last Post: 11-30-2006, 02:36 AM
  5. Any Ideas?
    By IanMarlowe in forum JavaScript
    Replies: 2
    Last Post: 07-24-2005, 03:59 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •