Monday 5 November 2018

javascript - Including a .js file within a .js file




I'd like to know if it is possible to include a .js file within another .js file?




The reason for me wanting to do this is to keep client includes to a minimum. I have several .js files already written with functions that are needed by the client. The client would have an html file which he/she manages with a .js file include (my .js file).



I could re-write a new .js file with all the functions in it or, to avoid doing double work, figure out a way to write a .js file that includes other .js files.


Answer



I basically do like this, create new element and attach that to



var x = document.createElement('script');
x.src = 'http://example.com/test.js';
document.getElementsByTagName("head")[0].appendChild(x);



You may also use onload event to each script you attach, but please test it out, I am not so sure it works cross-browser or not.



x.onload=callback_function;

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...