I am using code to add javascript to my page using javascript file..
My code is :
alert("works");
addScript('http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js');
addScript('js/pdttrack/jquery.titlealert.js');
addScript('js/pdttrack/jquery.msgbox.min.js');
function addScript(filename) {
var tagPosition = document.getElementsByTagName('body');
var scriptElement = document.createElement('script');
scriptElement.setAttribute('type', 'text/javascript');
scriptElement.setAttribute('src', filename);
tagPosition.appendChild(scriptElement);
alert("added "+ scriptElement);
}
…but the last alert is not firing.. seems
tagPosition.appendChild(scriptElement);
is not firing rest all are..
Try changing:
To:
Since
getElementsByTagNamereturnsNodeListwhich isArray-like and you need to specifyindex/keyto the array.You can also simply do:
Learn more on MDN: