Is there something in JavaScript similar to @import in CSS that allows you to include a JavaScript file inside another JavaScript file?
How do I include a JavaScript file in another JavaScript file
Sommaire |
Questions
Answers
http://exploringjs.com/es6/ch_modules.html http://exploringjs.com/es6/ch_modules.html
Ajax Loading
Load an additional script with an Ajax call and then use eval. This is the most straightforward way, but it is limited to your domain because of the JavaScript sandbox security model. Using eval also opens the door to bugs and hacks.
jQuery Loading
http://api.jquery.com/jQuery.getScript/ http://api.jquery.com/jQuery.getScript/
$.getScript("my_lovely_script.js", function(){ alert("Script loaded but not necessarily executed."); });
Dynamic Script Loading
Add a script tag with the script URL in the HTML. To avoid the overhead of jQuery, this is an ideal solution.
The script can even reside on a different server. Furthermore, the browser evaluates the code. The <script> tag can be injected into either the web page <head>, or inserted just before the closing </body> tag.
http://unixpapa.com/js/dyna.html http://unixpapa.com/js/dyna.html
Detecting when the script has been executed
Now, there is a big issue you must know about. Doing that implies that you remotely load the code . Modern web browsers will load the file and keep executing your current script because they load everything asynchronously to improve performance. (This applies to both the jQuery method and the manual dynamic script loading method.)
It means that if you use these tricks directly, you won t be able to use your newly loaded code the next line after you asked it to be loaded , because it will be still loading.
For example: my_lovely_script.js contains MySuperObject:
var js = document.createElement("script"); js.type = "text/javascript"; js.src = jsFilePath; document.body.appendChild(js); var s = new MySuperObject(); Error : MySuperObject is undefined
Then you reload the page hitting F5. And it works! Confusing...
So what to do about it ?
Well, you can use the hack the author suggests in the link I gave you. In summary, for people in a hurry, he uses an event to run a callback function when the script is loaded. So you can put all the code using the remote library in the callback function. For example:
function loadScript(url, callback) { // Adding the script tag to the head as suggested before var head = document.getElementsByTagName( head )[0]; var script = document.createElement( script ); script.type = text/javascript ; script.src = url; // Then bind the event to the callback function. // There are several events for cross browser compatibility. script.onreadystatechange = callback; script.onload = callback; // Fire the loading head.appendChild(script); }
http://en.wikipedia.org/wiki/Anonymous_function http://en.wikipedia.org/wiki/Anonymous_function
var myPrettyCode = function() { // Here, do what ever you want };
Then you run all that:
loadScript("my_lovely_script.js", myPrettyCode);
http://www.html5rocks.com/en/tutorials/speed/script-loading/ http://www.html5rocks.com/en/tutorials/speed/script-loading/
Source Code Merge
http://stackoverflow.com/q/5511989/59087 http://stackoverflow.com/q/5511989/59087
Source
License : cc by-sa 3.0
http://stackoverflow.com/questions/950087/how-do-i-include-a-javascript-file-in-another-javascript-file