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 6 of 6
  1. #1
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    817
    Thanks
    43
    Thanked 6 Times in 5 Posts

    about checking for null value

    I need to check to see if a cookie has been previously created:

    Code:
    var startnum = readCookie('a1');
    if (document.cookie.indexOf('a1') == -1){
    var startnum = "0";
    }
    Should line 2 be:

    Code:
    if (document.cookie.indexOf('a1') == null){
    ?

    Does == -1 do the same thing?
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    You could easily find out just by testing, but if the cookie is not found

    Code:
    var startnum = readCookie('a1');
    if (document.cookie.indexOf('a1') == -1){
    var startnum = "0";
    alert (startnum);
    }
    results in 0

    while

    Code:
    var startnum = readCookie('a1');
    if (document.cookie.indexOf('a1') == null){
    var startnum = "0";
    alert (startnum);
    }
    returns false.

    "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." - Brian W. Kernighan

  • #3
    Banned
    Join Date
    Nov 2008
    Location
    not found
    Posts
    284
    Thanks
    0
    Thanked 53 Times in 51 Posts
    Quote Originally Posted by tpeck View Post
    I need to check to see if a cookie has been previously created:

    Code:
    var startnum = readCookie('a1');
    if (document.cookie.indexOf('a1') == -1){
    var startnum = "0";
    }
    Should line 2 be:

    Code:
    if (document.cookie.indexOf('a1') == null){
    ?

    Does == -1 do the same thing?
    If you have a cookie-reading function, why use additional code to read document.cookie?

    Code:
    if ( !readCookie('a1') ){
    var startnum = "0";
    }
    However since conditional variable declaration can be hazardous:

    Code:
    var startnum = Number( readCookie('a1') ) || 0;

  • #4
    New Coder
    Join Date
    Jun 2010
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post
    This version fixes some fairly serious issues with the old version. Above script fails to handle cases where cookie names are substrings of other cookies, say like: 'a1', 'a12' and so on.
    Code:
    // this fixes an issue with the old method, ambiguous values
    // with this test document.cookie.indexOf( name + "=" );
    
    function Get_Cookie( check_name ) {
       // first we'll split this cookie up into name/value pairs
       // note: document.cookie only returns name=value, not the other components
    
       var a_all_cookies = document.cookie.split( ';' );
       var a_temp_cookie = '';
       var cookie_name = '';
       var cookie_value = '';
       var b_cookie_found = false; // set boolean t/f default f
       
       for ( i = 0; i < a_all_cookies.length; i++ )
       {
          // now we'll split apart each name=value pair, and get rid of any escapes
          a_temp_cookie = a_all_cookies[i].split( '=' );
          // and trim left/right whitespace while we're at it
          cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
          
          // if the extracted name matches passed check_name
          if ( cookie_name == check_name )
          {
             b_cookie_found = true;
             cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
             return cookie_value;
             break;
          }
          a_temp_cookie = null;
          cookie_name = '';
       }
       if ( !b_cookie_found )
       {
          return null;
       }
    }
    
    if (Get_Cookie( 'a1' )){ 
    ...................
    Thanks to ThoughtfulCoder for point this problem out.
    Nothing's imposible imagination is everything!
    Database Benchmark Software (GNU GPL) | world's fastest database

  • #5
    Banned
    Join Date
    Nov 2008
    Location
    not found
    Posts
    284
    Thanks
    0
    Thanked 53 Times in 51 Posts
    Code:
    function readCookie( cName )
    {
      var cValue = "";
    
      return ( cValue = document.cookie.match( "(^|;)\\s*" + cName + '=([^;]+);?') ) ? decodeURIComponent( cValue[ 2 ] ) : "" ;
    }

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Hmmm, good point, but it would be best to simply avoid naming a cookie with a name which is a substring of another cookie name. That is bound to cause confusion in any event.


  •  

    Posting Permissions

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