Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Get cookie values

    Hi,
    I am new to javascript and I am trying to develop a way to filter cookies on my site.
    As the user interacts with some of my site elements, I need certain cookies to change. I simply need a way to filter through my cookie list (document.cookie) to find a specific cookie name. I have this so far...

    Code:
    function valueget() {
    var findinstr = document.cookie;
    var tofindstr = document.getElementById('tofind').value;
    var findresult = '';
    var str_parts = findinstr.split(";");
    var i = 0;
    while (findresult==''||i<=str_parts.length-1) {
    if (str_parts[i].substring('','=')==tofindstr) {
    findresult = str_parts[i].substring('=','');
    }
    else {
    i = i + 1;
    }
    }
    if (findresult=='') {
    alert('Could not be found');
    }
    else {
    alert(findresult);
    }
    }
    and the HTML code is simply

    Code:
    <input type="text" id="tofind">
    <input type="button" value="Get Value" onclick="valueget()">
    Is this the best way to find a cookie name and value, or is there an easier way.

    By the way, every time I test this script, my page freezes

    Any help is appreciated

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by ABorrello View Post
    Is this the best way to find a cookie name and value, or is there an easier way.

    By the way, every time I test this script, my page freezes
    If you want to search, this topic has been covered zillions of times, although not always very well...

    The essential problem is your misuse of substring, which expects to work with integer parameters, not strings.

    The statement
    Code:
    if (str_parts[i].substring('','=')==tofindstr)
    could be something like
    Code:
    if( str_parts[i].substring( 0, str_parts[i].indexOf( '=' ) ) == tofindstr )
    although it is important to split on "; " not ";".

    Here is an example of using the String.split approach and also a RegEx example:
    Code:
    function readCookie( cName ) /* Uses String.split */
    {
      var pairs = ( document.cookie || "" ).split( "; " ), 
          len = pairs.length, 
          offset;  
       
      for( var i = 0; i < len && ( offset = pairs[ i ].indexOf( cName + '=' ) ) != 0; i++ )
      ;
     
      return  i != len ? decodeURIComponent( pairs[ i ].substring( offset + cName.length + 1 ) ) : "" ;   
    }
    
    
    function readCookie( cName ) /* Uses Regex */
    {
      var v;
    
      return decodeURIComponent( ( v = ( document.cookie || "" ).match( "(^|\\s)" + cName + "=([^;$]+)" ) ) ? v[ 2 ] : "" );
    }


  •  

    Tags for this Thread

    Posting Permissions

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