Is there something similar to @import
in CSS in JavaScript that allows you to include a JavaScript file inside another JavaScript file?
|
|||||||||||||
|
|
|||||
|
I also wrote a JavaScript dependency manager for Java web applications: JS-Class-Loader. |
||||
|
In case you are using Web Workers and want to include additional scripts in the scope of the worker, the other answers provided about adding scripts to the Fortunately, Web Workers have their own Alternatively, as the second highest voted answer to your question highlights, RequireJS can also handle including scripts inside a Web Worker (likely calling |
||||
|
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:
|
||||
|
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 am adding the statement you have mentioned in the top of my .js file:
You don't need an include. You can just add the code to the existing JavaScript file. Or you can call JavaScript files from the HTML file. |
||||
|
protected by NullPoiиteя Jun 10 '13 at 5:07
Thank you for your interest in this question.
Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?