Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is there something similar to @import in CSS in JavaScript that allows you to include a JavaScript file inside another JavaScript file?

share|improve this question
16  
6  
@Daniel, I do not want to use an AJAX call. –  Alec Smart Jun 4 '09 at 12:02
2  
Nonetheless the answers are the same. –  annakata Jun 4 '09 at 12:20
show 1 more comment

33 Answers 33

Here is my synchronous version without jQuery:

function myRequire( url ) {
    var ajax = new XMLHttpRequest();
    ajax.open( 'GET', url, false ); // <-- the 'false' makes it synchronous
    ajax.onreadystatechange = function () {
        var script = ajax.response || ajax.responseText;
        if (ajax.readyState === 4) {
            switch( ajax.status) {
                case 200:
                    eval.apply( window, [script] );
                    console.log("script loaded: ", url);
                    break;
                default:
                    console.log("ERROR: script not loaded: ", url);
            }
        }
    };
    ajax.send(null);
}
share|improve this answer
show 1 more comment

The @import syntax for achieving "css-like" js importing is possible using a tool such as Mixture via their special .mix file type (see here). I imagine the application simply uses one of the aforementioned methods "under the hood," though I don't know.

From the Mixture documentation on .mix files:

Mix files are simply .js or .css files with .mix. in the file name. A mix file simply extends the functionality of a normal style or script file and allows you to import and combine.

Here's an example .mix file that combines multiple .js files into one:

// scripts-global.mix.js
// Plugins - Global

@import "global-plugins/headroom.js";
@import "global-plugins/retina-1.1.0.js";
@import "global-plugins/isotope.js";
@import "global-plugins/jquery.fitvids.js";

Mixture outputs this as scripts-global.js and also as a minified version (scripts-global.min.js).

Note: I'm not in any way affiliated with Mixture, other than using it as a front-end development tool. I came across this question upon seeing a .mix javascript file in action (in one of the Mixture boilerplates) and being a bit confused by it ("you can do this?" I thought to myself). Then I realized that it was an app-specific file type (somewhat disappointing, agreed). Nevertheless, figured the knowledge might be helpful for others.

share|improve this answer
add comment

I am adding the statement you have mentioned in the top of my .js file.

document.write('<scr'+'ipt type="text/javascript" src="file2.js" ></scr'+'ipt>');

but why are you include? You don't need an include. You can just add the code to the existing JavaScript file. Or you can call js files from the HTML file.

share|improve this answer
add comment

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?

Not the answer you're looking for? Browse other questions tagged or ask your own question.