Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > BOOK: Beginning JavaScript
|
BOOK: Beginning JavaScript
This is the forum to discuss the Wrox book Beginning JavaScript by Paul Wilton; ISBN: 9780764544057
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning JavaScript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 5th, 2004, 12:35 AM
Registered User
 
Join Date: Jan 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default getting a cookie's value - 'preceeding space char'

In Wilton's discussion of getting a cookie's value (pages 426-7), he first searches the cookiename with a space preceding the cookieName, eg - cookieValue.indexOf(" " + cookieName + "="). If that returns
-1, he searches for the same cookieName without that space.

Why search for the cookie with that preceding space, at all? If I know that no space leads the cookieName of the cookie that I want, why then should I look for that cookie with preceding space? Beyond my control, does document.cookie add a space character to the beginning of the cookieName?

Please advise.

Thanks.

OC

 
Old January 5th, 2004, 06:32 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

To be honest, I wouldn't know. Doesn't the book or the comments explain his reasoning behind it?

AFAIK, no spaces are added automatically. So a cookie called MyCookie can always be retrieved with the same name. No need to check for additional spaces.....

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old January 5th, 2004, 09:36 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

In Javascript cookies are indeed separated by a space, like:name1=value1; name2=value2; name3=value3

Adding the space to the search string ensures that you only get a cookie with an exact match on the whole name.

I don't have the book so i'm assuming that the search is done again without the space in case its the first cookie in the list (coz then it wouldn't have the preceding space). Personally I add a space to the beginning of document.cookie and then do the search just once.

hth
Phil
 
Old January 5th, 2004, 12:04 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
quote:In Javascript cookies are indeed separated by a space, like:name1=value1; name2=value2; name3=value3
Right, I see now. For some reason I was thinking this was Server Side......

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old January 5th, 2004, 12:25 PM
Registered User
 
Join Date: Jan 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I understand now.

Thank you.

 
Old February 12th, 2004, 11:59 AM
Registered User
 
Join Date: Feb 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think the main reason for the presence of the blank space before the cookie name in the instruction cookieValue.indexOf(" " + cookieName + "=") is in the next example:

Let's suppose you have two cookies.

Cookie1 name = "SampleCookie"
Cookie2 name = "AnotherSampleCookie"

and let's suppose that the position in the cookie-string of "AnotherSampleCookie" is before "SampleCookie".

So, when you need to get the Cookie1, using the instruction without the initial blank space - cookieValue.indexOf(cookieName + "=") - you will get the Cookie2, making error.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Big challenge here! How to convert char* to char^? samiswt Visual C++ 2005 1 November 30th, 2007 09:09 PM
Please help with passing (char * name[]) pdang265 C++ Programming 1 September 26th, 2007 05:59 AM
Invalid conversion from 'char*' to 'char' deuxmio C++ Programming 3 December 8th, 2006 07:56 AM
copy value in char ct Excel VBA 1 October 27th, 2005 08:30 AM
Update adds a preceeding space dhborchardt Classic ASP Databases 2 April 7th, 2004 10:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.