0

I'm using the following code:

<script>
    var time = new Date();

    time.setFullYear(time.getFullYear() + 1, time.getMonth(), time.getDay());

    expires = ";expires=" + time.toGMTString();

    document.write(expires);

    document.cookie = "aardvark=cats; expires= " + time +"; path=/";

    document.write('\ndone: ' + document.cookie);
</script>

I've put it into a html file and ran it locally, and also tried running it on JSFiddle, and tried uploading it to my site and accessing it in my browser.

In internet explorer 9, no cookie is created on my computer. Any ideas why not?

NibblyPig
  • 46,891
  • 62
  • 180
  • 311

2 Answers2

1

When looking at your code, it might well be that this snippet gets executed after the DOM is fully loaded. Loading the dom implicitly closes the document, whereas document.write needs the document to be opened. If it isn't one of three things can happen:

  • Best case: nothing happens, nothing is written
  • Worse: A new page is created
  • Worst, and sadly most common: the existing document is overwritten

check this question on why document.write: is not to be used Why is document.write considered a "bad practice"?

As the accepted anser to the linked question also mentions (and since that fixed things for the OP, too) calling document.write on an XHTML dom simply doesn't work. Probably FF/Chrome/Chromium/Safari/Opera all render the DOM as HTML5 by default nowadays. IE9 will probably opt for XHTML transitional or something... explicitly setting a doctype seemed to be the fix for the op, so I'm guessing <!DOCTYPE html> was what did the trick

Community
  • 1
  • 1
Elias Van Ootegem
  • 67,812
  • 9
  • 101
  • 138
  • I don't think that's the case, I've tried putting the script into the HEAD tag. – NibblyPig Apr 05 '13 at 09:17
  • @SLC: if you see both lines that are sent to the document using `document.write`, then check your privacy settings (IE might be blocking the cookie), or check to see if the page is running in compat. mode... IE's older engine doesn't support localStorage. Also: try replacing the `document.write` with `document.body.innerHTML += expires + "
    ";` and so on...
    – Elias Van Ootegem Apr 05 '13 at 09:23
  • Here `document.write` is for just debug purpose. A talent like you should have guessed it already –  Apr 05 '13 at 09:24
  • @silentboy: are you _really_ that obtuse or just trolling for fun? sure it's a debugging stmt, as is a simple `var_dump` in PHP, but don't expect a `set_cookie` call to work after a dump: headers have been sent, means no cookies can be set. No matter _why_ a statement is used, it can have unforseen consequences... – Elias Van Ootegem Apr 05 '13 at 09:33
  • @silentboy: in order not to bother the OP with notices about new comments, I'll post mine here: It's not midnight here (AM vs PM -> 12 hours difference). w3schools is _really_ not good at all. This is my true name, and my only account. I try to be sensible, and I'm open to critisism... but you're just trolling now – Elias Van Ootegem Apr 05 '13 at 09:38
  • Javascript is not java not even php. You can set cookie `while(true)` times using javascript, any time on any event occured. If not just provd me wrong –  Apr 05 '13 at 09:39
  • @silentboy: `window.onload = function(){document.write('foobar'); document.cookie = 'never=set;' + (new Date()).toString() + ';path=/'; console.log(document.cookie);}`... in this example, the cookie won't be set, because the DOM (and with it the JS code) is cleared. That has been my point all the time. Open your console, and do a document.write call, whatever page you're on will be gone... I know JS isn't Java or PHP... stop trolling – Elias Van Ootegem Apr 05 '13 at 09:45
  • I just say that w3schools have a good example about the js cookie. I didnt say that it is a programmers bible. But i am really sorry for those spam. I want to remove the downvote and +1 your answer but my votes are locked. Consider editing your answer. Thanks –  Apr 05 '13 at 09:45
  • Nice explanation. Maybe ie 9 also render page in quirk mode if a doctype not given. Thanks –  Apr 05 '13 at 09:58
  • @silentboy: 2nd comment to this answer... I don't think IE switches to quirk when presented with doctype-less _valid_ markup, though... I believe compat IE7 will be used... still annoying as hell, of course :P – Elias Van Ootegem Apr 05 '13 at 10:02
0

I've fixed it now. Setting the DOCTYPE was required.

I assumed cookies existed since forever and would work using the default DOCTYPE but this wasn't the case.

NibblyPig
  • 46,891
  • 62
  • 180
  • 311
  • I've edited my answer. Since the link I provided mentions doctypes, too. Could you check to see if my edit comes close to the truth? Did you in fact set the HTML5 doctype? – Elias Van Ootegem Apr 05 '13 at 09:55
  • I'm merely quoting MDN: [check the last note](https://developer.mozilla.org/en-US/docs/DOM/document.write) – Elias Van Ootegem Apr 12 '13 at 10:07