简体   繁体   English

JavaScript包括

[英]JavaScript includes

I have 4 javascript files (each for a single HTML file) and 3 functions are THE SAME in all 4 files. 我有4个javascript文件(每个用于单个HTML文件),所有4个文件中有3个函数是相同的。 I'd like to find a smooth solution that I could somehow include these 3 functions separately... is it possible to include .js within a .js? 我想找到一个平滑的解决方案,我可以分别包含这三个函数...是否可以在.js中包含.js?

  1. You can have multiple <script> tags: 您可以拥有多个<script>标记:

     <script src="lib.js"></script> <script> // do stuff </script> 
  2. You can use jQuery : 你可以使用jQuery

     $.getScript("lib.js", function() { // do stuff }); 
  3. You can use a pre-processor like browserify or YUICompressor or Google's Closure Compiler . 您可以使用预处理器一样browserify的YUICompressor谷歌的关闭编译器

You can write this by your own, for example as it works in google analytics: 您可以自己编写,例如,因为它适用于谷歌分析:

(function(){
    var lastIncludedScript = document.getElementsByTagName('script')[0];
    var yourScript = document.createElement('script');
    yourScript.type = 'text/javascript';
    yourScript.src = 'path/to/script.js';
    lastIncludedScript.parentNode.insertBefore(yourScript, lastIncludedScript);
})();

or as in function: 或者在功能上:

function includeScript( path ){
    var lastIncludedScript = document.getElementsByTagName('script')[0];
    var yourScript = document.createElement('script');
    yourScript.type = 'text/javascript';
    yourScript.src = path;
    lastIncludedScript.parentNode.insertBefore(yourScript, lastIncludedScript);
}

usage: 用法:

includeScript( 'path/to/script.js' );

You can have a look at this nice explanation about loading javascript files asynchronously. 你可以看一下关于异步加载javascript文件的这个很好的解释 This could solve your problem. 这可以解决您的问题。 Or look at this stack overflow question on how to load javascript files inside other javascript files. 或者看看这个堆栈溢出问题,关于如何在其他javascript文件中加载javascript文件。

However it may be worth a shot to include everything you use in one single javascript file and load the same one on every site. 但是,在一个单独的javascript文件中包含您使用的所有内容并在每个站点上加载相同的文件可能值得一试。 This improves performance because only one http request has to be made to load all javascript and it can be cached very efficiently (so no need to load any javascript later on). 这样可以提高性能,因为只需要一个http请求来加载所有javascript,并且可以非常高效地缓存它(因此以后不需要加载任何javascript)。

Use proper js file required in each html file. 使用每个html文件中所需的正确js文件。 That will solve your issue. 这将解决您的问题。 Also the one contating the general functions can be added for both html files. 还可以为两个html文件添加一个包含一般功能的功能。 This should solve your problem. 这应该可以解决您的问题。

It's a bit slow to include files from other JavaScript in the browser. 在浏览器中包含来自其他JavaScript的文件有点慢。 I would write a server-side script to combine and minify all relevant JavaScript into one "file" to be sent to the client. 我会编写一个服务器端脚本来将所有相关的JavaScript组合并缩小为一个“文件”以发送给客户端。

Your pages can pass parameters to this script to choose what needs to be included. 您的页面可以将参数传递给此脚本以选择需要包含的内容。

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

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