javascript - Can't get cookies to set in IE9 -
i'm using 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 html file , ran locally, , tried running on jsfiddle, , tried uploading site , accessing in browser.
in internet explorer 9, no cookie created on computer. ideas why not?
when looking @ code, might snippet gets executed after dom loaded. loading dom implicitly closes document, whereas document.write
needs document opened. if isn't 1 of 3 things can happen:
- best case: nothing happens, nothing written
- worse: new page created
- worst, , sadly common: existing document overwritten
check question on why document.write
: not used why document.write considered "bad practice"?
as accepted anser linked question mentions (and since fixed things op, too) calling document.write
on xhtml dom doesn't work. ff/chrome/chromium/safari/opera render dom html5 default nowadays. ie9 opt xhtml transitional or something... explicitly setting doctype seemed fix op, i'm guessing <!doctype html>
did trick
Comments
Post a Comment