javascript - 为什么要避免页内 <script>

标签 javascript jquery html wordpress model-view-controller

<分区>

在处理网站中 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 - 为什么要避免页内 &lt;script&gt;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16159435/

相关文章:

javascript - KnockoutJS 和递归模板

javascript - 鼠标在屏幕左侧向左移动图像,鼠标在屏幕右侧时相同

javascript - 如何在angularjs自定义指令中获取模板中的属性值?

jquery - 固定页脚显示滚动条

internet-explorer - jquery ajax 在 IE 中不起作用

javascript - 当用户突出显示文本时如何防止模态显示?

Javascript 没有正确记录长度

javascript - 为什么这在 javascript 中有效?

javascript - 在使用 JavaScript 构建 HTML 时保持关注点分离的最佳实践是什么?

javascript - 单击显示 div 时出现错误