Your suggested approach is great for your situation. It has several advantages over more complex solutions, including complete simplicity, a completely predictable order of execution, and full compatibility with scripts that may not lend themselves to asynchronous loading. I used it many times in production applications, like many other developers.
Of course, other solutions have their advantages, but for what you are doing, there is nothing wrong with the good old document.write() .
It looks like you have several scripts and style sheets, and they are probably loaded from a shared directory (or shared root). To reduce the repetition in your framework.js file, you may need to define two functions: one for the <link> for CSS, and the other for writing the <script> for JavaScript. Thus, the framework.js skeleton file might look something like this:
(function() { var scriptBase = 'js/'; var styleBase = 'css/'; function writeStyle( name ) { document.write( '<link rel="stylesheet" href="', styleBase, name, '">', '</link>' ); } function writeScript( name ) { document.write( '<script src="', scriptBase, name, '">', '</script>' ); } writeStyle( 'one.css' ); writeStyle( 'two.css' ); writeScript( 'one.js' ); writeScript( 'two.js' ); })();
Note that you do not need to perform special text escaping </script> , as you can see in the code that uses document.write() . This is only necessary when you put this code directly inside the <script> in an HTML file. The purpose of this escaping is to prevent the <script> tag from closing </script> inside the call to document.write() . Since your code is in an external .js file, this is not a problem: the presence of text </script> inside this file will not lead to the completion of the .js file.
Another point to consider is that all document.write() calls that you make inside the .js file are inserted into the document after the .js file that writes them. So donβt expect that you can do document.write() inside framework.js and then add other code inside framework.js , which depends on the .js file you just wrote. All these .js files (and .css ) are loaded after framework.js , and not alternated with it.
Another consideration, of course, is boot time. Your page can load faster if you combine and reduce your CSS and JS files. But if these are internal web applications, page loading time may be the least of your worries: reliability and maintainability may be more important. And in any case, you can always use the document.write() solution to start and run right now, and then optimize it only in the unlikely event that you need.