admin管理员组文章数量:1323335
Consider the following document
<html>
<body>
This is some content
<script type="text/javascript" src="verySlowToRespond.js"></script>
This is some more content
</body>
</html>
I'd like first to check my assumption that it is unsafe for the browser to parse beyond the script
tag until the script has loaded and executed.
This means that (if my assumption is correct), say verySlowToRespond.js
takes 20 seconds to respond, the page DOM cannot be fully assembled until this dependency is resolved.
Supposing verySlowToRespond.js
hung about indefinitely? At what point would the browser give up and continue with the parse?
Consider the following document
<html>
<body>
This is some content
<script type="text/javascript" src="verySlowToRespond.js"></script>
This is some more content
</body>
</html>
I'd like first to check my assumption that it is unsafe for the browser to parse beyond the script
tag until the script has loaded and executed.
This means that (if my assumption is correct), say verySlowToRespond.js
takes 20 seconds to respond, the page DOM cannot be fully assembled until this dependency is resolved.
Supposing verySlowToRespond.js
hung about indefinitely? At what point would the browser give up and continue with the parse?
- 2 I don't have hard numbers, but Firefox's timeout is something between 20-30 seconds from my experience. This is a value set individually in each browser. – Pekka Commented Aug 2, 2010 at 13:53
- 2 This is one of the reasons why I avoid putting JS in the <body> and prefer to run scripts on load. – Robusto Commented Aug 2, 2010 at 13:55
3 Answers
Reset to default 3Correct: the browser won't continue beyond the script tag until it's read and evaluated it.
The browser gives up based on the same timeout rules it uses for pages. It depends on the browser, and on the exact nature of the timeout.
I'm wondering why there'd be such a thing as a slow-to-respond script. Is there something wrong with your hosting? Is the script slow to respond, or does it load and then take a really long time to run?
Yes your first assumption is correct. Browsers stop rendering until <script>
tags finishing loading AND executing.
The behavior on very long running scrips is browser-dependant. Newer browsers will often give you a chance to abort the script. Older browsers may need to be force-closed.
You are correct. The browser will not render the rest of the page until that script has finished. The timeout would be dependent on the underlying socket properties I suppose.
If you are concerned about the loading time of a script, it is important that the script not make any DOM changes that need to be executed before the rest of the page renders. Then you can add an onload
handler to call setTimeout
to execute code to load the script asynchronously.
本文标签: domLoad timeout for JavascriptStack Overflow
版权声明:本文标题:dom - Load timeout for Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742136944a2422411.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论