异步跨域加载js
分类:前端开发 2010/7/3 PM
从jQuery中剥离出来的,不依赖JQuery。
调用:new ET().getScript({url:url,callback:function(){}})
var ET = function(){
this.config = {
url : "",
charset : "utf-8",
callback :function(){}
}
}
ET.merge = function(d, s){
for(var k in s){d[k] = s[k]}
return d;
}
ET.prototype = {
getScript : function(config){
var _script, _callback;
this.config = ET.merge(this.config, config);
_script = document.createElement("script");
_script.setAttribute("type", "text/javascript");
_script.setAttribute("charset", this.config.charset);
_script.setAttribute("src", this.config.url);
document.body.appendChild(_script);
_callback = this.config.callback;
_script.onload = _script.onreadystatechange = function(callback){
if(!this.readyState || this.readyState=='loaded' || this.readyState=='complete'){
_callback();
}
}
}
}IE的 script 元素支持on
firefox的script 元素不支持on
发表评论: