admin管理员组文章数量:1291199
There are two events related to web page initialization of browser.
- DOMContentReady(document object) : HTML document was parsed and DOM tree was constructed
- load(window object) : All element of HTML document were rendered(displayed)
In my understanding, browser can't start rendering page before DOM is ready(DOMContentReady is fired) and by default script tag blocks any other browser process until script file is downloaded and executed.
Then why is it good to put script tag in the end of body tag? In my opinion, browser will be blocked when it meets script tag in any position of the page, so DOMContentReady will not be fired until script tag is downloaded and executed. As a result, user can't see anything except white blank page until script is fully processed regardless of position of script tag.
There are two events related to web page initialization of browser.
- DOMContentReady(document object) : HTML document was parsed and DOM tree was constructed
- load(window object) : All element of HTML document were rendered(displayed)
In my understanding, browser can't start rendering page before DOM is ready(DOMContentReady is fired) and by default script tag blocks any other browser process until script file is downloaded and executed.
Then why is it good to put script tag in the end of body tag? In my opinion, browser will be blocked when it meets script tag in any position of the page, so DOMContentReady will not be fired until script tag is downloaded and executed. As a result, user can't see anything except white blank page until script is fully processed regardless of position of script tag.
Share Improve this question asked Jun 17, 2013 at 4:23 firia2000firia2000 1,9235 gold badges20 silver badges22 bronze badges 2- 2 If you've ever have the pleasure of using painfully slow internet, you'll often see partially-rendered pages. Browsers (at least Chrome and Firefox) re-render the page as the DOM is built so that it looks like it loads faster. – Blender Commented Jun 17, 2013 at 4:25
- 1 see developer.yahoo./blogs/ydn/… – Arun P Johny Commented Jun 17, 2013 at 4:26
1 Answer
Reset to default 9Modern browsers work by downloading the page and working through the elements and content as it arrives. When the browser encounters script elements it will work to download those in the order they appear, and it will avoid rendering further content until after those scripts are downloaded in case they need to be present first. This a blocking operation, unlike e.g. a reference to an image.
This means if your script elements are at the start of the body or in the header, your browser needs to download those before any body content at all begins to show up. Your users might wait a while watching a blank screen wondering what's going on and wondering if anything's working at all. Web users tend to only wait a few seconds (about 10) before concluding a site won't load—justifiably since working sites tend to also load quickly, and the ones that take ages are usually unusable.
We resolve this situation using one (or both) of two tools:
- We put the script elements at the end of the body, after all of the page's contents. This means the entire page will display as soon as it's available, and then the scripts will download to make things work.
- As of HTML5, we can mark scripts as async:
<script src="..." async></script>
. This instructs the browser to download the script in the background while continuing to download and render the page, without waiting on the script to resolve before displaying stuff.
In my understanding, browser can't start rendering page before DOM is ready (...) As a result, user can't see anything except white blank page until script is fully processed regardless of position of script tag.
This isn't the case. Modern browsers will often display the content of even a partially-loaded page even while it's still being downloaded, in order to give users something to look at. It means if you're downloading a 10,000 word essay over a slow connection, you can at least get started on the first few paragraphs while the rest of the page loads.
本文标签: javascriptWhy is it good to put script tag in the end of body tagStack Overflow
版权声明:本文标题:javascript - Why is it good to put script tag in the end of body tag? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741507958a2382434.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论