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 baffled by responses to this question. I had to use a variable (myVar1) defined in one JS file (myvariables.js) in another JS file (main.js). For this I did as below:
File: myvariables.js
File: main.js
As you saw, I had use variable in one JS file in another JS file This saved my day. 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 questions. 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 $.getScript and really any other solution that requires a callback when loading is complete is that if you have multiple files that use it and depend on each other you no longer have a way to know when all scripts have been loaded. (Once they are nested in multiple files.) ex: 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 $.when to check an array of deferred objects, but now you are doing this in every file and file2 will be considered loaded as soon as the $.when is executed not when the callback is executed so file1 still continues execution before file3 is loaded. This really still has the same problem. I decided to go backwards instead of forwards. Thank you document.writeln. I know its taboo, but as long as it is used correctly this works well. You end up with code that can be debugged easily, shows in the DOM correctly and can ensure the order the dependencies are loaded correctly. You can of course use $("body").append(), but then you can no longer debug correctly anymore. 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 pretty sure it'll fail. I liked the idea of extending JQuery, but obviously you don't need to. Before calling document.writeln, it checks to make sure the script has not already been loading by evaluating all the script elements. I assume that a script is not fully executed until its document.ready event has been executed. (I know using document.ready is not required, but many people use it, handling this is a safeguard.) When the additional files are loaded the document.ready callbacks will get executed in the wrong order. To address this when a script is actually loaded, the script that imported it is re-imported itself and execution halted. This causes the originating file to now have it's document.ready callback executed after any from any scripts that it imports. Instead of this approach you could attempt to modify the JQuery readyList but this seemed like a worse solution. solution:
usage: file3:
file2:
file1:
|
|||
|
I basically do like this, create new element and attach that to head.
in jquery:
|
|||
|
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 have the requirement to load async an array of javascripts and at the final make a callback. Basically my best approach is the following:
Example:
The second script don't will load until the first is completely loaded, and so... Results: |
|||||
|
You can't import, but you can reference. PhpShtorm IDE. To reference, in one
Of course, you should use you own PATH to JS file. I don`t now will it work at others IDE. Probably yes, just try. It should work in Visual Studio too. |
||||
|
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
|
|||||||||
|
|
|||||||||||||||||
|
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