Appending JavaScript file reference after the last hard coded JavaScript file reference in the HEAD
Apologies if the following sounds a little strange. Am working in a legacy framework that allows no access to core templates so cannot alter the loading order of files.
What i am trying to do is load a JS file in the HEAD of the document after the final SCRIPt in said document HEAD.
I currently am using the following with no success:
var head = document.getElementsByTagName("head")[0];
var headScripts = head.getElementsByTagName("script");
var headScriptsLength = headScripts.length - 1;
var headScr开发者_JAVA百科ipts = headScripts[headScriptsLength];
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = '/Global/ICIS/Scripts/global.js';
headScripts.appendChild(newScript);
It adds the reference to global.js directly after the block that creates it.
You better off appending your script simply to the head, and so it will become your last script.
var head = document.getElementsByTagName("head")[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = '/Global/ICIS/Scripts/global.js';
head.appendChild(script);
精彩评论