動(dòng)態(tài)加載外部javascript文件
只是做個(gè)記錄,以后不要自己滿街找代碼。。。
(function (clover) {
clover.loadScript = function loadScript(url, callback) {
var heads = document.getElementsByTagName('head');
if (heads.length == 0) {
alert("page must have one head element");
}
var head = heads[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
// most browsers
script.onload = callback;
// IE 6 & 7
script.onreadystatechange = function () {
if (this.readyState == 'complete') {
callback();
}
}
head.appendChild(script);
}
})(window.clover = window.clover || {});
// sample
// clover.loadScript("http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js");
沒(méi)經(jīng)過(guò)優(yōu)化, 其實(shí)應(yīng)該cache一點(diǎn)東西,并且堅(jiān)持當(dāng)前的script是不是存在的
posted on 2011-07-27 22:39 聽說(shuō)讀寫 閱讀(514) 評(píng)論(2) 收藏 舉報(bào)
浙公網(wǎng)安備 33010602011771號(hào)