简体   繁体   English

从多个文件加载单个javascript文件

[英]Loading a single javascript file from multiple files

I'm trying to load a single javascript in pieces by calling the javascript from external separate files, and was wondering the best way to go about doing this. 我试图通过从外部单独的文件中调用javascript来分段加载单个javascript,并且想知道这样做的最佳方法。 Specifically, this is a just a basic google maps page, and I want to organize the code a little better. 具体来说,这只是一个基本的Google Maps页面,我想更好地组织代码。 I'm hoping to split the marker variables up into groups and store those groups of variables in separate files, then call those files within the main javascript header of the page. 我希望将标记变量分成几组,并将这些变量组存储在单独的文件中,然后在页面的主要javascript标头中调用这些文件。 I want to restrict this code to just html and javascript to maintan its simplicity for the purpose of future updates by individuals less than knowledgeable in this area. 我想将此代码限制为仅html和javascript,以保持其简单性,以供日后对此领域知识不足的个人进行更新。 I don't do a whole lot of coding with JavaScript so, if there already is a built-in function for this, that would be great. 我不会使用JavaScript进行大量编码,因此,如果已经有内置的功能,那就太好了。 This is purely aesthetic, just to make the code a little cleaner. 这纯粹是为了美观,只是为了使代码更简洁。 Any help or suggestions would be appreciated. 任何帮助或建议,将不胜感激。 Thanks. 谢谢。

You can add references to external files: 您可以添加对外部文件的引用:

<html>
  <head>
    <script type="text/javascript" src="colorGradient.js"></script>
    <script type="text/javascript" src="xpath.js"></script>
    <script type="text/javascript" src="kml2.js"></script>
    <style type="text/css"> ... </script>
  </head>
  <body>
    ....
  </body>
</html>        

You can try a "feature loading" and/or "on-demand javascript loading" framework. 您可以尝试“功能加载”和/或“按需javascript加载”框架。 Since you're trying to use Google maps, I would recommend you use the Google Loader API which works very closely to what you're seeking. 由于您尝试使用Google地图,因此建议您使用Google Loader API ,该API与您要查找的内容非常接近。

for example: With a simply JS you can do the following.... 例如:使用一个简单的JS,您可以执行以下操作。

<script type="text/javascript">
  google.load("search", "1");
  google.load("jquery", "1.4.2");
  google.load("jqueryui", "1.7.2");
</script>

... and it will load the multiple files. ...,它将加载多个文件。

If I understand you right, you don't want to call one JavaScript files from several another JavaScript files. 如果我理解正确,则您不想从多个其他JavaScript文件中调用一个JavaScript文件。 You want just save some groups of variables. 您只想保存一些变量组。 Well, you can save it - with a server-side database or, may be, with http://www.w3.org/TR/webstorage/ or http://www.w3.org/TR/IndexedDB/ 好吧,您可以保存它-使用服务器端数据库,也可以使用http://www.w3.org/TR/webstorage/http://www.w3.org/TR/IndexedDB/

Splitting up you code for your development is a good idea. 为您的开发拆分代码是一个好主意。 There for, there are a lot of frameworks to help you organize your code with the help of MVC and psudo MVC models. 为此,有很多框架可以帮助您借助MVC和psudo MVC模型来组织代码。

Try: 尝试:

http://maccman.github.com/spine/ http://maccman.github.com/spine/

or 要么

http://documentcloud.github.com/backbone/ http://documentcloud.github.com/backbone/

But what prevents you from having everything in one file on the production environment? 但是,是什么导致您无法将所有内容都存储在生产环境中的一个文件中? Its easier on the server requests. 它更容易在服务器上请求。 And you want to integrate this in your build process anyway... 而且您仍然希望将其集成到构建过程中...

But if you insist in on adding them to the DOM you can do this of course: 但是,如果您坚持要将它们添加到DOM中,则可以这样做:

document.write(unescape('%3Cscript src="yourfile.js"%3E%3C/script%3E'))

this will add 这将添加

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

to your dom 到你的家

If I understand your question correctly, you would like to split up a single script into multiple .js files. 如果我正确理解了您的问题,则希望将单个脚本拆分为多个.js文件。 As far as I know, this should work fine as long as you include a tag to load each file. 据我所知,只要您包含用于加载每个文件的标签,此方法就可以正常工作。 You may need to load the files in order (ie don't include a file that calls a function that has not been difeined yet). 您可能需要按顺序加载文件(即,不包括调用尚未定义的函数的文件)。

However, be aware that splitting up your script will result in more calls to the server, which will slow down page load. 但是,请注意拆分脚本会导致对服务器的更多调用,这将减慢页面加载速度。 In most cases, just including a few scripts won't even make a noticible difference in load time. 在大多数情况下,仅包含几个脚本甚至不会在加载时间上产生明显的差异。

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

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