使用事件控制,Ext异步加载JS,兼容IE与FF

//自定义类继承 Ext.util.Observable
ModuleLoader = function(config) {  
    // /属性构建  
    Ext.apply(this, config);  
    this.addEvents({  
    	"loaded" : true  
    });  
}  
Ext.extend(ModuleLoader, Ext.util.Observable, { 
	dom: new Array(),
	loadedCount: 0,
    // /定义属性及其默认参数和事件      
    load: function(path) {  
   		var th = this;
      	if(path.script){
          	if(!path.script.pop) path.script = [path.script];  
           //创建结点;
        for (var i=0; i<path.script.length; i++) {
        	 //alert("path.script[i]=" + path.script[i]);    
             this.dom[i] = document.createElement("script");  
             this.dom[i].src = path.script[i];  
             document.getElementsByTagName("head")[0].appendChild(this.dom[i]);                     
         }  
         
         //注册事件;
         for(var i=0; i<this.dom.length; i++){
         	if(Ext.isIE){
         		this.dom[i].onreadystatechange = this.fireLoaded.createDelegate(this);
         	}else{  
            	this.dom[i].onload = this.fireLoaded.createDelegate(this);
            }	
         }

       }			        
    },
    fireLoaded: function(){
    	for (var i=0; i<this.dom.length; i++) {
    		if (this.dom[i].readyState && this.dom[i].readyState == "loading")   
                return;  
    		else
    			this.loadedCount++;				    		
    	}  

        
        if(this.loadedCount >= this.dom.length)
        {   
            this.fireEvent('loaded', this);
            this.dom = new Array(); 
        }                              	
    }         	
}); 

 

调用:

var loaderJs;
Ext.onReady(function(){	
	loaderJs = new ModuleLoader();
	loaderJs.on("loaded", initMainPage);
	loaderJs.load({
		script:[
			'../plugin/building/js/zh_cn/building_adslot.js',
			'../plugin/building/js/zh_cn/building_content.js',
			'../plugin/building/js/zh_cn/building_admtask.js',
			'../plugin/building/js/zh_cn/BuildingResource.js'
		]
	});
});

function initMainPage()
{		
	loaderJs.un("loaded", initMainPage);
//添加你下面要做的代码

}

//参照:http://aidxn527.iteye.com/blog/508377

你可能感兴趣的:(IE,ext,Blog)