Possible Duplicate:
Include JavaScript file inside JavaScript file?
I am looking for semething that can include JavaScript file inside another JavaScript file. Something that would have the same value as @import in CSS. Is-it possible ? Thx.
I am looking for semething that can include JavaScript file inside another JavaScript file. Something that would have the same value as @import in CSS. Is-it possible ? Thx. |
|||||
|
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
There are basically 3 common approaches to cope with your problem. 1) The typical approach for this is to append a script to the head, like this:
2) Alternatively, there are script loaders like jQuery's
3) A third way is to use asynchronous script loaders like requirejs, so you will end up using this:
Generally speaking, the people at requirejs have solved lots of problems (cross browser stuff) for you, so this might be the best way to go. Update An other way came to my mind, which is so nasty that I forgot it first:
Just mentioned it for the sake of completeness. |
|||||||||||
|
You can use RequireJS (requirejs.org). |
|||
|
jQuery...
|
|||
|
I feel this question deserves some open mindedness, so rather than just listing some answers, I have added some insight and considerations for issues which may arise relating to this, so please note, I know this may not be exactly what is being asked, but, hopefully goes above and beyond the call of duty. I guess you can look at this question from two perspectives. 1. From a development perspective: Say for example you are developing a JavaScript based application (web or otherwise), and you have several script files. You may want to "include" these files (like "using" in .NET or "import" in java), so that you can see the prototypes and objects you have created in other files and their behavior, as you write your code. Unfortunately, JavaScript does not support such a feature, however some of this can be achieved by good JavaScript editors. WebStorm by JetBrains provides various mechanisms for being able to see prototypes and objects from different scripts across a project, and in my opinion, WebStorm is an exceptionally good editor, which provides good intellisense, multi-script development, code validation etc...and comes at a very reasonable price. Visual Studio has also come a long way in this field. The intellisense has improved vastly over the past couple of versions, and you can use something called VSDoc, which you can "include" in your script file to provide documentation regarding prototypes and objects in other JavaScript files. Here is an example of using VSDoc in a Visual Studio JavaScript file.
2. From a release & run perspective: Now you get to the point that you want to release & run your application. VSDoc no longer works because this is something that is implemented in Visual Studio, and is not supported by JavaScript implementations. Technically there would be no point in supporting such a feature as VSDoc only documents what a JavaScript file can do. It does not handle anything to do with the implementation of the script. Equally, all of the "helping hand" functionality you get in WebStorm no longer works as it's implemented as part of the WebStorm IDE, not as a JavaScript standard. So, when it comes to releasing and running your JavaScript, you have the following options (which have already been pointed out. I'm just adding them for the sake of completeness)
Unfortunately, as I have already pointed out, the idea of referencing in JavaScript is currently un-implemented (and would certainly be a nice feature in ECMAScript-6 or above)! These workarounds seem to be the best solutions given the lack of standardized functionality! Considerations: If you are writing large JavaScript based applications, you might want to consider using Dart, TypeScript or CoffeeScript, all of which are super-sets of JavaScript. Dart and TypeScript are very new to the development scene, and provide things like classes and type inference, both of which are lacking in current JavaScript implementations. The beauty of these languages is that they compile to pure JavaScript, so you can still run your application on a JavaScript engine. |
|||
|