简体   繁体   English

为不同的页面加载不同的js文件或一起加载?

[英]load different js files for different pages or load together?

I have 3 pages of site. 我有3页的网站。

Page 1: 19 kb of JS 第1页:JS的19 kb
Page 2: 26 kb of JS 第2页:JS的26 kb
Page 3: 10 kb of JS 第3页:JS的10 kb
Total : 55 kb of JS 总计:JS的55 kb

These javascript files are non repeating, means the JS needed on page 1 is not needed on page 2 and I have expiry headers set 1 month. 这些javascript文件是不可重复的,这意味着不需要页面1上的JS,而页面2上也不需要,而且我的有效标头设置为1个月。

Still I would like to know what will be best way to load these files, should I load separate file for each page or I load these all together? 我仍然想知道什么是最好的加载这些文件的方法,我应该为每个页面加载单独的文件还是将它们全部加载在一起?

you should probably load them separately... 您可能应该分别加载它们...
But, in order to speed things up you could do a trick : if you think that the user is staying for a bit (i donno, at least 5 sec) on your page, you could just load the script for that particular page and add the other ones remotely after the page loads. 但是,为了加快处理速度,您可以采取以下措施:如果您认为用户在页面上停留了一段时间(我不知道,至少5秒钟),则只需加载该特定页面的脚本并添加页面加载后,其他远程访问。 This way you force the client's browser to make a cache copy of your other - not needed at the moment - js files, and because they're being loaded after the dom object has been built, it doesn't slow your page rendering. 这样,您可以强制客户端的浏览器为您的其他文件(目前尚不需要)创建js文件的缓存副本,并且由于它们是在构建dom对象之后加载的,因此不会降低页面渲染的速度。
You will have to add an "addScript" function and make "addScript" calls when the document has finished loading. 文档加载完成后,您将必须添加“ addScript”函数并进行“ addScript”调用。 For the first js (for the first page) it should be something like : 对于第一个js(对于第一页),它应该类似于:


function addScript(jsUrl){
    var s = document.createElement('script');
    s.setAttribute('type','text/javascript');
    s.setAttribute('src',jsUrl);
    document.getElementsByTagName('body')[0].appendChild(s);
}
window.onload = function(){
    addScript('mySecondScript.js');
    addScript('myThirdScript.js');
}
The beauty is that when you load one of the other pages, the corresponding js file is loaded instantly because it is retrieved from the browser's cache. 这样做的好处是,当您加载其他页面之一时,会立即加载相应的js文件,因为它是从浏览器的缓存中检索到的。

I'd mash them all together in one JS file, minify that with Google's Closure Compiler set on "advanced optimizations", and load it once with a bunch of cache directives. 我将它们全部合并到一个JS文件中,并通过在“高级优化”中设置Google的Closure Compiler使其最小化,然后使用一堆缓存指令将其加载一次。 If you do use the Closure Compiler set on advanced, you'll have to make sure there is no inline JavaScript in your HTML files (which is generally not a good practice anyway), because the function and variable names (even the global functions and variable names) won't be the same afterward. 如果确实使用高级设置的Closure Compiler设置,则必须确保HTML文件中没有内联JavaScript(无论如何这通常不是一个好习惯),因为函数和变量名(甚至是全局函数和变量名)将不会相同。

55 kb isn't all that much. 55 kb并不那么多。 However, it may be harder to update Page 2 when you have to deal with page 1 and 3 code. 但是,当您必须处理第1页和第3页代码时,更新第2页可能会比较困难。 Also, have you tried minifying the code that sounds like rather large javascript files. 另外,您是否尝试过缩小听起来像是很大的javascript文件的代码。

The way you described it, you should load them separately. 按照您描述的方式,应该分别加载它们。

If the user is guaranteed to navigate to all 3 pages then yes, combine them. 如果保证用户可以导航到所有3页,则可以,将它们合并。 If they usually navigate to one page, then loading all three is a waste. 如果他们通常导航到一页,那么加载全部三个页面是浪费的。

That said, 55k is not THAT large, however consider the impact from a mobile browser or even dial-up. 也就是说,55k并不大,但是请考虑移动浏览器甚至拨号的影响。

Minimize and use compression to reduce the JS to the maximum, but don't load a file unless it is required. 最小化并使用压缩来最大程度地减少JS,但是除非需要,否则不要加载文件。

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

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