...

View Full Version : about checking for null value



tpeck
06-18-2010, 12:09 PM
I need to check to see if a cookie has been previously created:



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


Should line 2 be:


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

Does == -1 do the same thing?

Philip M
06-18-2010, 12:34 PM
You could easily find out just by testing, but if the cookie is not found


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

results in 0

while


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

randomuser773
06-18-2010, 01:55 PM
I need to check to see if a cookie has been previously created:



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


Should line 2 be:


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?



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


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

saviola
06-18-2010, 02:43 PM
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.


// 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 (http://techpatterns.com/forums/post-2902.html)for point this problem out.

randomuser773
06-18-2010, 04:41 PM
function readCookie( cName )
{
var cValue = "";

return ( cValue = document.cookie.match( "(^|;)\\s*" + cName + '=([^;]+);?') ) ? decodeURIComponent( cValue[ 2 ] ) : "" ;
}

Philip M
06-18-2010, 05:06 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum