<分区>
在处理网站中 javascript 的结构时。目前我尝试将我所有的 javascript 分离到文件中,比如 app.js。
尽管将脚本放在它们出现的实际页面上并带有标签会更好吗?如果是,原因是什么?
<分区>
在处理网站中 javascript 的结构时。目前我尝试将我所有的 javascript 分离到文件中,比如 app.js。
尽管将脚本放在它们出现的实际页面上并带有标签会更好吗?如果是,原因是什么?
最佳答案
一般的经验法则是将脚本文件放在外部文件中。 Yahoo Developer网站还要求这样做:
Using external files in the real world generally produces faster pages because the JavaScript and CSS files are cached by the browser. JavaScript and CSS that are inlined in HTML documents get downloaded every time the HTML document is requested. This reduces the number of HTTP requests that are needed, but increases the size of the HTML document. On the other hand, if the JavaScript and CSS are in external files cached by the browser, the size of the HTML document is reduced without increasing the number of HTTP requests.
关于javascript - 为什么要避免页内 <script>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16159435/