var script = document.createElement('script');
if (script.readyState && !script.onload) {
// IE, Opera
script.onreadystatechange = function() {
if (script.readyState == "loaded" || script.readyState == "complete") {
script.onreadystatechange = null;
alert('Loaded');
}
}
}
else {
// Rest
script.onload = function() {
alert('Loaded');
};
}
script.src = src;
document.getElementsByTagName('head')[0].appendChild(script);
Можно оформить в функцию:
var loadScript = function(src, callback, appendTo) {
var script = document.createElement('script');
if (!appendTo) {
appendTo = document.getElementsByTagName('head')[0];
}
if (script.readyState && !script.onload) {
// IE, Opera
script.onreadystatechange = function() {
if (script.readyState == "loaded" || script.readyState == "complete") {
script.onreadystatechange = null;
callback();
}
}
}
else {
// Rest
script.onload = callback;
}
script.src = src;
appendTo.appendChild(script);
}