Home javascript How do I connect a .js file to another .js file?

How do I connect a .js file to another .js file?

Author

Date

Category

How do I connect a .js file to another .js file? I do not use JavaScript inside the html page, I use it as a language for writing mobile applications for the DroidScript tool. And since I have been programming in Python for a long time, I am used to dividing the code into useful modules, and in general it is more convenient to work with small files than with three-story mega-codes. Is there a way out?


Answer 1, authority 100%

function include (url) {
    var script = document.createElement ('script');
    script.src = url;
    document.getElementsByTagName ('head') [0] .appendChild (script);
  }

Call:

include ("/ js / script.js");

Answer 2, authority 44%

Suddenly a good question. There is no explicit support for inclusions, because the authors imply linking via an HTML script tag.

However, all this can be bypassed one way or another:

Option 1: jQuery one-liner

$. getScript ("another_script.js", function () {alert ("Loaded.");
// use the script here
});

Option 2 : It looks a little crazy at first glance, but you can dynamically create the script tag as suggested by @Hermann Zheboldov

True, all of this is useless if you do not have a browser that processes HTML.

So I only see two options. Use m4 preprocessor or regular C preprocessor for linking, or somehow insert HTML processing code there and use the options above (this option is much more difficult).

P.S. Perhaps there is already some solution to the space with inclusions, it’s worth checking it out, since I have nothing to do with DroidScript and don’t know anything about it.


Answer 3, authority 11%

Build with collector, for example:

https://github.com/bem/borschik

Accordingly, there will be some one file with inclusions, on which you will set the collector and receive the assembled file at the output.


Answer 4

I program myself on DroidScript, and even write the engine on it. To connect the script, use the app.LoadScript method (‘script name.js “, callback); Instead of callback, you can insert the name of the function that will be executed after the script is loaded, but you can just specify the name of the script without adding callback

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions