"Michael Winter" <M.Winter@blueyonder.co.uk.invalid> wrote in message
news:Xns943998947850AMWinterBlueyonder@193.38.113. 46...[color=blue]
> David Graham wrote on 20 Nov 2003:
>[color=green]
> > Hi Mike
> > I'm learning cookies for my site. I would be grateful if you
> > could explain some of your code
> >
> > 1. !document.cookie.length - is this saying do something if
> > there is no cookie, established by the fact that no cookie with
> > a name that is longer than zero can be found? (may be way off
> > here in my interpretation)[/color]
>
> Almost. The document.cookie property returns a String containing
> *all* cookie name=value pairs. 'length' above is a String property
> that contains the length of the String. The expression evaluates to
> true if there are no cookies whatsoever associated with the domain of
> your site (document.cookie is zero-length). That is why I said that
> if you do use other cookies, this wouldn't be sufficient. It has
> other possible pitfalls, so I would recommend the second method.
>[color=green]
> > 2. document.cookie = 'popup=no'; - is this setting the name of the
> > cookie to 'popup=no', I think I'm totally off here, that would be a
> > job for setcookie wouldn't it?[/color]
>
> Yes, this creates a cookie called 'popup' with a value of 'no', and
> no expiry date (so it expires when all browser windows are closed).
> If the setcookie that you use is like this one below (from Netscape's
> JavaScript Guide), then yes, you could use setcookie.
>
> // Sets cookie values. Expiration date is optional
> //
> function setCookie(name, value, expire) {
> document.cookie = name + "=" + escape(value)
> + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
> }
>[color=green]
> > 3. if ( -1 == document.cookie.search( /popup=no/i )) - is this
> > something like if the expression on the right is equivalent to
> > false then do something. But I am confused about details like the
> > forward slashes and the letter 'i' at the very end of the line.[/color]
>
> As I said earlier, document.cookie returns a String, and 'search' is
> a method of String. It uses a "regular expression" to find a match
> and returns the index of that match. If there is no match, it returns
> -1. The method call above will return -1 if the string returned by
> document.cookie doesn't contain "popup=no".
>
> The slash syntax is used to create a regular expression literal, just
> like quotes create string literals. The 'i' at the end makes the
> search case-insensitive. Regular expressions are very powerful and
> can be very confusing to look at. You should be able to find detailed
> descriptions on how to use them in a good JavaScript reference. If
> you're interested, try one of Netscape's guides here:
>
>
http://devedge.netscape.com/library/...ript/1.3/guide
> /regexp.html
>
> Hope that clears things up. Feel free to ask anything else if not.
>[/color]
Hi Michael
Thanks for your expert replies - most appreciated. There is one thing I
would like to ask.
// If no cookie has been set...
if ( !document.cookie.length )
{
// ...show pop-up and set a dummy cookie
document.cookie = 'popup=no';
}
You say in your reply
"The expression evaluates to true if there are no cookies whatsoever
associated with the domain of
your site (document.cookie is zero-length). " Why then would you want to
turn the true to a false by putting an exclamation mark in the if
statement? - by doing that, the lines belonging to the if statement will not
be executed and you do want them to be executed if no cookie has been set.
thanks for your efforts on my behalf
David