admin管理员组文章数量:1404063
Would a page load faster if I keep all my JavaScript in a separate file say MyCode.js
and include that file rather than have the JavaScript included in the html file?
Or does it not really matter?
Would a page load faster if I keep all my JavaScript in a separate file say MyCode.js
and include that file rather than have the JavaScript included in the html file?
Or does it not really matter?
Share Improve this question edited Mar 20, 2015 at 12:11 Pedro del Sol 2,8419 gold badges41 silver badges52 bronze badges asked Sep 28, 2010 at 9:37 EtienneEtienne 7,20143 gold badges110 silver badges163 bronze badges4 Answers
Reset to default 5It very rarely matters for speed purposes unless you're Google. However, for readability and maintainability, it's much better to keep your JavaScript in external files.
If you put your <script>
tags at the end of the page (just before the closing <body>
tag) the JavaScript won't block loading of the page.
There are benefits for both inline and included script files.
Inline scripts get loaded with the HTML therefore only one HTTP request is required to download both this saves time, the resulting HTML file will be bigger but you do not waste time with the handshake and headers.
Included scripts get loaded as a separate HTTP request and therefore there is this overhead, however, included scripts get cached by the browser which means subsequent pages do not need to request this resource from the server again.
As a rule, include inline scripts wherever those scripts directly relate to the content of the page that is being requested, you will save HTTP requests for the page and you do not need to cache the scripts separately. For site wide scripts, always include these as separate script files, these will get downloaded once the first time they are needed and then a cached copy will be used for subsequent pages.
If you've got plenty of scripts, there might be some to be saved by keeping js in external files as it allows the browser to do a better job of caching your script files. Same goes for CSS.
But generally, meh. The obvious advantage of externalising is maintainability.
Depending on JS code size. If You have huge JS code part then putting everything separately has few pluses:
You can split different file types among different domain names which can increase page load time - while most of browsers limit to 4 concurrent connections to the same domain.
It is easier to maintain
本文标签: htmlExternal or Inline JavaScriptPage Load TimeStack Overflow
版权声明:本文标题:html - External or Inline JavaScript? - Page Load Time - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744231043a2596335.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论