DoomedLung said:
Hey, I recently bought "The Javascript and DHTML Cookbook"
by Danny Goodman.
Hard luck.
I'm at Chapter 1, part 1.9 "Reading and Writing Strings
for Cookies" and was just interested in a utility function
that grabs a cookie value:
function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1) {
endstr = document.cookie.length;
}
return unescape(document.cookie.substring(offset, endstr));
}
The part I don't understand is the if statement within the
function, why is it checking if endStr equals to false?
First -1 is not a false value. That is, if used in a context where a
boolean value is required a number type-converts to boolean as; zero and
NaN convert to boolean false, all other numbers (including -1) convert
to boolean true.
Type-converting comparison (with - == -, as opposed to - === -) does not
type-convert to boolean. It prefers to do its comparisons on numeric
value (with strings its second choice). No type-conversion is needed
above as the value of - endstr - will be numeric if the code gets to
that line at all.
The reason for the text is that the - indexOf - method of stirngs
returns the index of the argument string within the subject string as an
index. To signal that the test string could not be found it returns the
number -1.
Cookies are name/value pairs in the form - name=value -, separated with
a semicolon. Searching a string for a semicolon (from a start index)
will give the point where the name/value pair ends. However, if the next
name value pair is either the only one, or the last one, it will not be
followed by a semicolon. In that case the - indexOf - method returns -1.
As this condition occurs when the name/value pair is the last in the
cookie, the only one in the cookie or the cookie is empty, if there is a
name/value pair its end coincides with the end of the string, so the end
index is set to the length of the string.
The code above fails to take into account a characteristic of content
inserting/re-writing proxies (such as personal firewalls and Internet
security programs) with some 'privacy' setting enabled. Software of this
type may modify incoming javascript code to replace occurrences of the
character sequence 'cookie' with another character sequence. The results
is that code that attempts to interact with cookies in the browser may
find themselves re-written in such a way as to make - document.cookie -
into a reference to a value that is not a string (most likely the
undefined value). Such code will error-out if it tries -
document.cookies.indexOf - as you cannot call the - indexOf - method on
a value that is not either a string primitive or a string object.
This type of proxy software is sufficiently common (and not sufficiently
understood by its users) that it is a good idea guard against code
erroring-out when attempting to interact with cookies by verifying
that - document.cookie - does refer to a sting, using a test such as -
if('' == typeof document.cookie){ ... } - around the code that attempts
to read the cookie.
The book is kinda vague in explaining what this means!
It is not in the nature of 'cookbook' style books to be explaining what
they do. They are targeted at people who don't what to learn what they
are doing.
Richard.