(Note: most of the following was written before you said that you use the HTML file locally in a mobile browser without using a web server. Most of them are still used, some of them do not work, but I left it for others who really make webpages.)
1) Is there a way in which we can embed .js files inside HTML after the page is loaded for the first time?
Yes, it is really very simple (example: run / change ):
var script = document.createElement('script'); script.src = "path/to/the/file.js"; document.body.appendChild(script);
Note that the script will load asynchronously (you cannot just consider it loaded after calling appendChild ).
But if your goal is only to show the page while the 27 MB file is loading, you can simply put your script tag at the end of your page immediately before the closing </body> . Update . If you are using a local HTML file and not a web page, I think that is all you need: one script tag at the end of the page loading your 27MB.js.
2) What would be an optimistic solution for including so much JavaScript content in a web page?
Ideally, reduce the size, if at all possible. If you can claim assets as needed (using the above technique or ajax ), do it instead. Update . If you use a local file, not a web page, you basically cannot do ajax reliably. But you can demand - download what you need when you need it, adding script elements as described above.
Regarding your 27 files 1 MB. If you tightly bind script tags to them, It is better to make one 27 MB file than 27 1 MB files. Minimizing HTTP requests to your server (ideally in most .js files and one .css file) is one of the key ways to improve page load time. In your case, however, you said that the various parts are not needed until various objects are pressed, so you will probably get the main file (which, we hope, will be a lot less than 27 MB), and then a bunch of other things that you require at boot (as above).
Other things you can do:
- Minify , compress, or "compile" your .js files (this means that you will have separate "source" and "production" files, since this is usually a one-way process that removes comments, etc.)
- Make sure that your server serves .js files with gzip compression (for example, with Apache you will use mod_deflate ); you can check that it works with various tools
It’s also very worth reading: “Best practices to speed up your site” , which makes the above points and the whole group.