Hey guys, thanks for the info. document.write sure is a strange beast...
On Feb 1, 5:44 pm, Juriy Zaytsev <[email protected]> wrote: > On Sat, Jan 29, 2011 at 11:56 AM, mcot <[email protected]> wrote: > > Hi, I am having a little trouble understanding some code from the > > prototype library: > > > (function() { > > /* Support for the DOMContentLoaded event is based on work by Dan > > Webb, > > Matthias Miller, Dean Edwards and John Resig. */ > > > var timer; > > > function fireContentLoadedEvent() { > > if (document.loaded) return; > > if (timer) window.clearInterval(timer); > > document.fire("dom:loaded"); > > document.loaded = true; > > } > > > if (document.addEventListener) { > > if (Prototype.Browser.WebKit) { > > timer = window.setInterval(function() { > > if (/loaded|complete/.test(document.readyState)) > > fireContentLoadedEvent(); > > }, 0); > > > Event.observe(window, "load", fireContentLoadedEvent); > > > } else { > > document.addEventListener("DOMContentLoaded", > > fireContentLoadedEvent, false); > > } > > > } else { > > document.write("<script id=__onDOMContentLoaded defer src=//:><\/ > > script>"); > > $("__onDOMContentLoaded").onreadystatechange = function() { > > if (this.readyState == "complete") { > > this.onreadystatechange = null; > > fireContentLoadedEvent(); > > } > > }; > > } > > })(); > > > On IE6, the last part is executed because IE uses attachEvent instead > > of addEventListener. I am confused about the document.write() of a > > script with defer followed by the $(<some element>). That would imply > > the script tag is written/added to the DOM immediately. > > > Typically when a document.write() occurs in the head section, I have > > noticed that anything that is written is not immediately available: > > > <html> > > <head> > > <script type="text/javascript" src="https://getfirebug.com/firebug- > > lite.js"></script> > > <script> > > > document.write("<div id=test></div>") > > console.log(document.getElementById("test")) //this is null > > > </script> > > </head> > > <body> > > <script> > > console.log(document.getElementById("test")) //this returns the DOM > > node with id test. > > </script> > > </body> > > </html> > > > It looks like scripts (and not just deferred ones) are available > > immediately: > > > <html> > > <head> > > <script type="text/javascript" src="https://getfirebug.com/firebug- > > lite.js"></script> > > <script> > > > document.write("<script id=test defer src=//:><\/script>"); > > document.write("<script id=test2><\/script>"); > > console.log(document.getElementById("test")) // Script DOM Node > > console.log(document.getElementById("test2")) // Script DOM Node > > > </script> > > </head> > > <body> > > > </body> > > </html> > > > My theory here is that this is the HTML parser deciding which types of > > elements are allowed in the HEAD section, and then waiting until the > > BODY section to "write" anything not allowed in the HEAD section. > > Just wondering if anyone had any additional insight. > > > Thanks in advance. > > [...] > > I've done similar tests in the past > —http://kangax.github.com/jstests/document_write_script_execution_orde... > > As you can see, (inline) scripts do in fact get executed in a recursive > manner. This behavior is the same in all browsers I tested. > > -- > kangax -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
