简体   繁体   English

将小型js文件合并为一个较大的文件

[英]Merge small js-files into one bigger

I want to tidy up the js-code used on my php-website to increase the loading speed. 我想整理我的php网站上使用的js代码,以提高加载速度。 For the moment i include in every website the required js-file. 目前,我在每个网站中都包含必需的js文件。 My plan is to merge all js-files into one big one. 我的计划是将所有js文件合并为一个大文件。 Not every page uses every js-code, so i started something but don't know if this makes any sense. 并非每个页面都使用每个js代码,因此我开始了一些工作,但不知道这是否有意义。

I have already read the article One JS File for Multiple Pages but the method of Paul Irish is way to complicated for me (for the moment) as a beginner. 我已经阅读了一篇“多页一个JS文件”的文章但Paul Irish的方法对于我(对于现在)初学者来说是很复杂的。

This is my approach: I create the file core.js and call it on every website like.. 这是我的方法:创建文件core.js并在每个网站上都调用它。

<script src="js/core.js"></script>  

In core.js i first get the name of the corresponding page. 在core.js中,我首先获取相应页面的名称。

var path = window.location.pathname;
var page = path.split("/").pop(); 
var page_name = page.slice(0, -4); 

Then i check which site requires which js-script (pseudo-code). 然后,我检查哪个站点需要哪个js脚本(伪代码)。

if (page_name == 'xyz'){
execute this code which is only used on this site
}

if (page_name == 'abc' || 'xyz' || 'def'){
execute another code which is used on multiple sites
}

if (page_name == 'ghi' || 'jkl' || 'mno' || 'xyz'){
include jquery for multiple sites
}
...
...

This means a lot of work for me, because i have a lot of js, so i wanted to ask first if this is a good solution to tidy up. 这对我来说意味着很多工作,因为我有很多js,所以我想先问一下这是否是整理的好解决方案。

By the way: The js code i place on my website doesn't change often. 顺便说一句:我放置在我的网站上的js代码不会经常更改。

Thank you 谢谢

Misch 米施

Split your javascript into sensible groups. 将您的javascript分成明智的小组。 You may have an admin section to your site, so have admin.js . 您可能在网站上有一个admin部分,所以也有admin.js

It's also worth noting that most browsers will only download the javascript file once and then cache it. 还值得注意的是,大多数浏览器只会下载一次javascript文件,然后将其缓存。 You said that your code does not change very often so you may find that putting it all in one file doesn't actually have that much of an affect. 您说过,您的代码不会经常更改,因此您可能会发现将它们全部放在一个文件中实际上没有太大影响。

Lets say you have pages like page1, page2, page3 etc. 假设您有诸如page1,page2,page3等页面。

Then your core.js will include all the codes of all the pages and then just initialize the code which you want to use 然后您的core.js将包含所有页面的所有代码,然后只需初始化要使用的代码

var page1= (function () {
              var Init = function (){
              //write the codes used by page 1
              };
              return {
                Initialize: function () {
                  Init();
                 }
              };
            })();

var page2= (function () {
              var Init = function (){
              //write the codes used by page 2
              };
              return {
                Initialize: function () {
                  Init();
                 }
              };
            })();
var page3= (function () {......});

var page = path.split("/").pop(); 
var path = window.location.pathname;

var page = path.split("/").pop(); 
var page_name = page.slice(0, -4); 

if (page_name == 'pg1'){
     page1.Initialize();
}

if (page_name == 'pg2' || 'pg3'){
     page2.Initialize();
     page3.Initialize();
}

if (page_name == 'pg4' ){
     page4.Initialize();
}

A solution for your problem could be something like: 解决问题的方法可能是:

if(selector) {
 //run code
}

This runs the code inside the block only if a particular selector exists. 仅当存在特定选择器时,这才在块内运行代码。 This way you don't have to go through all the trouble of getting the name of the page, splitting and slicing the string etc (this is also prone to errors). 这样一来,您不必麻烦获取页面名称,拆分和切片字符串等麻烦(这也容易出错)。

So let's say you want to add some innerHTML on some node it will look something like this: 假设您要在某个节点上添加一些innerHTML ,它看起来像这样:

function bar (text) {
  alert(text)
}

if(document.getElementById('#foo')) {
   bar('#foo exists!')
}

This way bar is only called when a node with id #foo exists. 仅当ID为#foo的节点存在时,才调用此方式bar

To be honest, this is just going to slow down your performance. 老实说,这只会减慢您的表现。 If your users stay a long time on the website, then one single file reduces a bit of clutter in your code. 如果您的用户在网站上停留了很长时间,那么一个文件就可以减少代码中的混乱情况。 But, if the user is visiting only a few pages, single file is just extra burden on bandwidth. 但是,如果用户仅访问几页,则单个文件只是带宽的额外负担。 There is a possibility, most of your users might not even need more than half of your js. 有一种可能,您的大多数用户可能甚至不需要超过一半的js。

Plus, those extra conditions aren't really helping anyone. 此外,这些额外条件并不能真正帮助任何人。 So, I would say, don't use single file option. 因此,我要说的是,不要使用单个文件选项。

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

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