admin管理员组文章数量:1336148
I have a big group of websites which has an include() on the <head>
tag, this include loads the content from a file which contains javascript code, this code uses document.write().
The code on the browser looks something like this:
<html>
<head>
<script>document.write('stuff');</script>
</head>
<body>
content
</body>
</html>
This seems to work fine but I was wondering if it could fail on some browser/puter, since the head tag is before the body tag, and document.write writes to the body tag. Is it possible that document.write would try to write to the body tag when this tag still wasn't loaded?
I have a big group of websites which has an include() on the <head>
tag, this include loads the content from a file which contains javascript code, this code uses document.write().
The code on the browser looks something like this:
<html>
<head>
<script>document.write('stuff');</script>
</head>
<body>
content
</body>
</html>
This seems to work fine but I was wondering if it could fail on some browser/puter, since the head tag is before the body tag, and document.write writes to the body tag. Is it possible that document.write would try to write to the body tag when this tag still wasn't loaded?
Share Improve this question asked Jun 4, 2014 at 18:58 user3571891user3571891 1111 gold badge2 silver badges6 bronze badges 2- 3 You could easily make a test case for this, you know – Sterling Archer Commented Jun 4, 2014 at 19:00
-
2
The best advice to give is not to use
document.write
. It is considered obsolete and "dangerous" as it by-passes the idea of the DOM. – Ingo Bürk Commented Jun 4, 2014 at 19:03
2 Answers
Reset to default 4Does document.write from head tag need to wait for window.onload?
No, quite the opposite.
The load event will fire when the document is closed. You can't write to a closed document, so calling write
would implicitly call open
and erase the existing document.
This seems to work fine but I was wondering if it could fail on some browser/puter, since the head tag is before the body tag, and document.write writes to the body tag.
write
will write to whereever the script element is, not to the body.
In this case, however, you are writing text content (which cannot appear as a child node to the head element).
This will cause the head element to end (the end tag is optional) and the body to start (the start tag is optional) and the content to be written at the beginning of the body.
</head>
<body>
will then be treated as invalid HTML and the browser will do its usual efforts to recover from author errors.
You can see the results in a DOM inspector:
Here is a small test case that shows the answer:
<html>
<head>
<script>
document.write('stuff');
console.log("document has written from head");</script>
</head>
<body>
<script>
window.onload = function() {
console.log("on load function has triggered");
}
</script>
</body>
</html>
And the output from Chrome Dev Tools console:
document has written from head test.html:3
on load function has triggered test.html:8
So yes, the code in the head section will run before the onload function.
Please note that document.write()
is not something you want to be practicing with. In my experience, I've never had to use it. It overwrites the DOM and can be rather nasty if you don't know how it works. You should use the DOM instead.
本文标签: javascriptDoes documentwrite from head tag need to wait for windowonloadStack Overflow
版权声明:本文标题:javascript - Does document.write from head tag need to wait for window.onload? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742246318a2439748.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论