Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-16-2012, 03:34 PM   PM User | #1
ABorrello
New to the CF scene

 
Join Date: Dec 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
ABorrello is an unknown quantity at this point
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
ABorrello is offline   Reply With Quote
Old 12-16-2012, 04:10 PM   PM User | #2
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 960
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
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 ] : "" );
}
Logic Ali is offline   Reply With Quote
Reply

Bookmarks

Tags
changing, cookies, dynamic, freezing, values

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 11:21 PM.


Advertisement
Log in to turn off these ads.