I am writing small JavaScript code which will load external js files at html page loading.
I tested 2 ways.
Case 1: Used document.write to add &
At the time when your javascript is evaluated, the DOM isn't fully built, the head element is not even finished. You normally cannot access or manipulate the DOM ( getElementsByTagName
and appendChild
, etc. ) before the HTML page is fully loaded.
Try this instead:
var s = document.createElement('SCRIPT');
s.charset = 'UTF-8';
s.src ='foo.js';
document.getElementsByTagName('HEAD')[0].appendChild(s);
But place this in a SCRIPT tag at the beginning of the BODY tag