The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

infos@wais-fr.com (29) [Avatar] Offline
#1
This script always return false
benvinegar (68) [Avatar] Offline
#2
Re: Listing 6.1 JavaScript to test whether or not cookies are enabled
Hey Henri,

Are you sure? I just ran the code sample in my JavaScript console on this web page, and it returned true. This is the expected result, since I'm attempting to set a first-party cookie (the same domain as the requesting page).

- Ben
infos@wais-fr.com (29) [Avatar] Offline
#3
Re: Listing 6.1 JavaScript to test whether or not cookies are enabled
Hi Ben,

I hope this demonstration will convince you that even if the result is good for you, the code cannot work as expected.

When Cookies are enabled, the following code:
allCookies(i).indexOf('test=1') always returns zero.

So, in this case, the code snippet is equivalent to : if (0) return true;

BUT 0 is the same as false (or maybe I am a stupid developer)
I have refactored the code snippet that works either in IE9 and Chrome 16:
https://gist.github.com/1622352
benvinegar (68) [Avatar] Offline
#4
Re: Listing 6.1 JavaScript to test whether or not cookies are enabled
Ah – I purely just looked to see whether the script ought to return false. I'll take a look tonight.
benvinegar (68) [Avatar] Offline
#5
Re: Listing 6.1 JavaScript to test whether or not cookies are enabled
Ah, rookie mistake - I forgot to do indexOf('...') !== -1.