简体   繁体   English

HTML内嵌的JavaScript

[英]javascript embedded inside html

I have a web application that is currently split into like 40+ javascript files. 我有一个Web应用程序,当前已分成40多个javascript文件。 When I run the application some subset of those files need to be downloaded by the browser. 当我运行该应用程序时,浏览器需要下载这些文件的某些子集。 Obviously, given that browsers use ~6 threads to download files, this is not the optimal solution. 显然,鉴于浏览器使用〜6个线程下载文件,所以这不是最佳解决方案。 One optimization idea that come to my mind was to embed all those javascript files (except external ones) inside the served .aspx page. 我想到的一种优化想法是将所有这些javascript文件(外部文件除外)嵌入到服务的.aspx页面中。 So that the browser just gets one big html file and does not need to make any round trips to the server. 这样浏览器就可以获取一个大的html文件,而无需往返服务器。 The html page alone may contain user specific data that will be different on every request. 单独的html页面可能包含特定于用户的数据,每个请求的数据都会有所不同。 The scripts, however, are not changing between requests. 但是,脚本在请求之间不会更改。 In typical use case the page (complete with scripts) has 180KB (scripts not minified) or 130KB (minified). 在典型的用例中,页面(包含脚本)具有180KB(未压缩脚本)或130KB(已压缩)。

Now the question: does this approach have any drawbacks performance wise (network, browsers' javascript engines)? 现在的问题是:这种方法在性能方面是否有缺点(网络,浏览器的JavaScript引擎)? Do you know of any big applications doing something like that? 您是否知道任何大型应用程序都在做类似的事情? Note that I am not interested in arguments about eg. 注意,我对例如eg的论点不感兴趣。 maintainability as the individual scripts will still be available as separate files during development. 可维护性,因为在开发过程中各个脚本仍将作为单独的文件提供。 Same question applies to css files (even though this is less of an issue in my app). 同样的问题也适用于css文件(即使这在我的应用程序中没什么问题)。

One bit of information that may be important here: the application is one big multipage form that does not require postbacks to go between the pages, validate form, submit the form, etc. However, the application in which it is embedded may have multiple such forms. 一点在这里可能很重要的信息:应用程序是一种大型的多页表单,不需要回发在页面之间,验证表单,提交表单等。但是,嵌入该应用程序的应用程序可能有多个这样的表单形式。

In general, it is a very good idea to concatenate javascript and css files together. 通常,将javascript和css文件连接在一起是一个很好的主意。 I'm just not so sure about "your concept". 我对“您的概念”不太确定。 My biggest concern and question here would be, can that .aspx file change potentially in any way through (dynamic) code ? 我最大的担心和问题是,.aspx文件是否可以通过(动态)代码以任何方式更改? That would make in impossible for the browser to cache the file, which would be a horrible scenario. 这将使浏览器无法缓存文件,这将是一个可怕的情况。

The great thing about concatenating files is, that we have one big downstream (which still is a lot faster then downloading with several single requests and HTTP overhead) and the browser can cache this file afterwards. 串联文件的好处是,我们有一个很大的下游文件(与通过几个单个请求和HTTP开销进行下载相比,它仍然要快得多),并且浏览器随后可以缓存该文件。

There are some great build-tools and scripts available, Apache ANT is one I can really suggest. 有一些很棒的构建工具和脚本,Apache ANT是我真正建议的工具。 You should have a look on the HTML5 Boilerplate where they make usage of ANT very frequently. 您应该看看HTML5 Boilerplate ,他们经常使用ANT。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM