最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to split JavaScript code into multiple files and use them without including them via script tag in HTML? - Stack Overflow

programmeradmin3浏览0评论

I am making use of constructors (classes) extensively and would like each constructor to be in a separate file (something like Java). Suppose I have constructors say Class1, Class2, ... Class10 and I only want to use Class1 and Class5 I need to use script tags to include Class1.js and Class2.js into the HTML page. Later if I also need to use Class3 and Class6 I again need to go to the HTML page and add script tags for them. Maintenance with this approach is too poor.

Is there something in JavaScript similar to include directive of C? If not, is there a way to emulate this behavior?

I am making use of constructors (classes) extensively and would like each constructor to be in a separate file (something like Java). Suppose I have constructors say Class1, Class2, ... Class10 and I only want to use Class1 and Class5 I need to use script tags to include Class1.js and Class2.js into the HTML page. Later if I also need to use Class3 and Class6 I again need to go to the HTML page and add script tags for them. Maintenance with this approach is too poor.

Is there something in JavaScript similar to include directive of C? If not, is there a way to emulate this behavior?

Share Improve this question asked Jun 3, 2011 at 12:08 CrackerCracker 1,7684 gold badges24 silver badges34 bronze badges 2
  • 1 "without including them via script tag in HTML" - Not possible (XMLHttpRequest?) – Shaz Commented Jun 3, 2011 at 12:12
  • 2 @Shaz I mean I only include one file, say main.js, and it loads all the required ones. – Cracker Commented Jun 3, 2011 at 12:14
Add a ment  | 

6 Answers 6

Reset to default 2

You can use jQuery.getScript:

http://api.jquery./jQuery.getScript

Or any of the many javascript loaders like YUI, JSLoader, etc. See parison here:

https://spreadsheets.google./lv?key=tDdcrv9wNQRCNCRCflWxhYQ

You can use something like this:

jsimport = function(url) {
    var _head = document.getElementsByTagName("head")[0];         
    var _script = document.createElement('script');
    _script.type = 'text/javascript';
    _script.src = url;
    _head.appendChild(_script);
}

then use it in your code like:

jsimport("example.class.js");

Be careful to use this when the head is already in the DOM, else it won't work.

Yes: You can create script tags from JavaScript and load required classes on demand.

See here for a couple of solutions: http://ntt/2008/02/10/4-ways-to-dynamically-load-external-javascriptwith-source.html

With careful use of id attributes or a global variable that contains "already loaded" scripts, it should be possible to develop a dependency resolution framework for JavaScript like Maven or OSGi for Java.

When we are talking about JavaScript, I feel it is better to include one file that includes everything you need instead of requesting a new file every time you need something that you don't currently have access to.

Each time you send out for another file, the browser will do many things. It checks if the requested file can in fact be found by sending an HTTPRequest, and if the browser has already seen this, is it cached and unchanged?

What you are wanting to do is not in the spirit of JavaScript. Doing what you are explaining will produce addition load times, and you wouldn't be able to do anything until the file has pletely loaded, which creates wait times.

It would be better to use one file for this, include at the inner end of the </body tag (which won't cause the browser to wait until the script is done to load the page), then create one simple function that will execute when the page is pletely loaded.

For example:

<html>
     <head></head>
     <body>
          <!-- HTML code here... -->
          <script src="javascript.js"></script>
          <script>
               (function r(f) {
                    /in/.test(document.readyState) ? setTimeout('r(' + f + ')', 9) : f()
               })(function() {
                       // When the page has pletey loaded
                       alert("DOM has loaded and is ready!");
               });
          </script>
     </body>
</html>

you can include one js file into another js file by doing something like this in the begginig of your js file:

 document.write("<script type='text/javascript' src='another.js'></script>");

The best approach in your situation is using of piler of some kind. The greatest one is Google Closure Compiler. This is part of Google Closure Libraty which has structure similar to what you described.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论