应该所有的JavaScript去到一个单独的js文件 [英] should all javascript go into a separate js file

查看:83
本文介绍了应该所有的JavaScript去到一个单独的js文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的标准是什么,如果一定的jquery或常规的JavaScript应该去内联或单独的js文件?

What is the criteria if certain jquery or regular javascript should go inline or in a separate js file?

推荐答案

这取决于很多因素,

1。缓存

当你分开你的JavaScript或CSS到单独的文件,那么它会在浏览器缓存,当一个新的请求到达时没有必要下载从浏览器一个新的。但在一个内联的每个请求页面的内容将被下载的时间,这将增加带宽使用的编码的情况下

When you separate your javascript or css into separate files then it will be cached in the browser and when a new request arrives there is no need to download a new one from the browser. But in the case of an inline coding each time the page is requested the content will be downloaded which will increase the bandwidth usage.

阅读更多 请JavaScript和CSS外部

2。减少HTTP请求

通过使一个内联的编码就可以减少这是一个页面优化技术的HTTP请求的数目。

By making an inline coding you can reduce the number of HTTP requests which is one page optimization technique.

了解更多关于这最小化HTTP请求

Read more on this in Minimize HTTP Requests

3。可维护性

通过使外部JavaScript和CSS文件时,它会更容易保持code。您不必T改变每个页面要应用的变化。

By making external javascript and css file it will be easier to maintain the code. You don't have t change each page for the changes to be applied.

4。微小

缩小为code删除不必要的字符,以减少其大小从而提高加载时间的做法。当code已经过压缩所有评论将被删除,以及不必要的空白字符(空格,换行,选项卡)。在JavaScript的情况下,这改善了响应时间性能,因为所下载的文件的大小被减小。

Minification is the practice of removing unnecessary characters from code to reduce its size thereby improving load times. When code is minified all comments are removed, as well as unneeded white space characters (space, newline, and tab). In the case of JavaScript, this improves response time performance because the size of the downloaded file is reduced.

阅读更多 压缩JavaScript

在这里,我发现了一个很好的文章

Here I found a nice article on

增压的Javascript

Supercharged Javascript

这篇关于应该所有的JavaScript去到一个单独的js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆