Is there something in JavaScript similar to @import
in CSS that allows you to include a JavaScript file inside another JavaScript file?
Join them; it only takes a minute:
|
||||
Here is a Grunt plugin allowing you to use It can be installed with npm install: https://npmjs.org/package/grunt-import |
||||
|
Keep it nice, short, simple, and maintainable! :]
This code is simply a short functional example that could require additional feature functionality for full support on any (or given) platform. |
|||
|
I had a simple issue, but I was baffled by responses to this question. I had to use a variable (myVar1) defined in one JavaScript file (myvariables.js) in another JavaScript file (main.js). For this I did as below: Loaded the JavaScript code in the HTML file, in the correct order, myvariables.js first, then main.js:
File: myvariables.js
File: main.js
As you saw, I had use a variable in one JavaScript file in another JavaScript file, but I didn't need to include one in another. I just needed to ensure that the first JavaScript file loaded before the second JavaScript file, and, the first JavaScript file's variables are accessible in the second JavaScript file, automatically. This saved my day. I hope this helps. |
|||||
|
Don't forget to check out LAB.js!
|
|||||
|
|
|||||||||
|
I also wrote a JavaScript dependency manager for Java web applications: JS-Class-Loader. |
||||
|
There are a lot of potential answers for this question. My answer is obviously based on a number of them. Thank you for all the help. This is what I ended up with after reading through all the answers. The problem with Example:file3.js
file2.js:
file1.js:
You are right when you say that you could specify Ajax to run synchronously or use XMLHttpRequest, but the current trend appears to be to deprecate synchronous requests, so you may not get full browser support now or in the future. You could try to use I decided to go backwards instead of forwards. Thank you You can of course use $("body").append(), but then you can no longer debug correctly any more. NOTE: You must use this only while the page is loading, otherwise you get a blank screen. In other words, always place this before / outside of document.ready. I have not tested using this after the page is loaded in a click event or anything like that, but I am pretty sure it'll fail. I liked the idea of extending jQuery, but obviously you don't need to. Before calling I assume that a script is not fully executed until its When the additional files are loaded the Instead of this approach you could attempt to modify the jQuery Solution:
Usage: file3:
file2:
file1:
|
||||
|
Another approach is to use HTML imports. These can contain script references as well as stylesheet references. You can just link an HTML file like
Within the
Look at https://www.html5rocks.com/en/tutorials/webcomponents/imports/ for more details. Unfortunately this only works in Chrome. |
||||
|
I basically do it like the following, creating a new element and attach that to head:
In jQuery:
|
||||
|
Here is maybe another way! In Node.js you do that just like the following! http://requirejs.org/docs/node.html sub.js
main.js
|
|||||
|
or
|
|||||||||
|
You can't import, but you can reference. PhpShtorm IDE. To reference, in one
Of course, you should use your own PATH to the JavaScript file. I don't know if it will work in other IDEs. Probably yes, just try. It should work in Visual Studio too. |
||||
|
I have the requirement to asynchronously load an array of JavaScript files and at the final make a callback. Basically my best approach is the following:
Example:
The second script will not load until the first is completely loaded, and so... Results: |
|||||
|
|
|||||||||||||||||
|
protected by NullPoiиteя Jun 10 '13 at 5:07
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
script
tags? – falsarella Jan 6 '15 at 21:07