自己写了个ScriptLoader

今天写了个ScriptLoader,可以在客户端动态加载Jscript,内部维护了一个Jscript队列,支持向队列里添加\删除Jscript,修改某个Jscript,并且防止了Jscript的重复.
有两种加载模式:
1.加载队列里所有的Jscript;
2.加载某一特定id的Jscript

window.ScriptLoader  =   new   function ()
{
    
var queuedScripts = new Array();
    
var currentScript = null;

    
function onScriptLoaded()
    
{
        
//Load next
        doScriptLoad();            
    }


    
function onScriptError()
    
{
        
//Invoke the error handler
        if (this.errorHandler)
            
this.errorHandler.invoke();

        
//Load next
        doScriptLoad();
    }


    
function onScriptReadyStateChange()
    
{
        
if(event.srcElement.readyState == "complete" || event.srcElement.readyState == "loaded")
            onScriptLoaded();
    }


    
this.loadScript = function(id,scriptPath,scriptText,loadall)
    
{
        
if(id != null)
            
this.addScript(id,scriptPath,scriptText)
        doScriptLoad(id);
    }


    
this.addScript = function(id,scriptPath,scriptText)
    
{
        
for(var i=0; i<queuedScripts.length; i++)
        
{
            
if(queuedScripts[i].id === id)
            
{
                alert(
"已有相同id的Script Tag");
                
break;
            }

        }


        queuedScripts.push(
new ScriptBlock(id,scriptPath,scriptText));
    }


    
this.alterScript = function(id,scriptPath,scriptText)
    
{
        
for(var i=0; i<queuedScripts.length; i++)
        
{
            
if(queuedScripts[i].id === id)
            
{
                queuedScripts[i].scriptPath 
= scriptPath;
                queuedScripts[i].scriptText 
= scriptText;
                
break;
            }

        }

    }


    
this.removeScript = function(id)
    
{
        
for(var i=0; i<queuedScripts.length; i++)
        
{
            
if(queuedScripts[i].id === id)
            
{
                queuedScripts.splice(i,
1);
                
break;
            }

        }

    }


    
//Does the actual script loading
    function doScriptLoad(id)
    
{
        
if (queuedScripts.length == 0)
            
return;

        
var obj = null;
        
var currentScript = null;
        
if(id == null || document.getElementById(id) == null)
        
{
            obj 
= queuedScripts.pop();
            
if(obj == nullreturn;
            currentScript 
= document.createElement("SCRIPT");
            currentScript.id 
= obj.id;
            currentScript.type 
= "text/javascript";
            currentScript.onload 
= onScriptLoaded; //For Mozilla/Opera
            currentScript.onerror = onScriptError; //For Mozilla/Opera
            currentScript.onreadystatechange = onScriptReadyStateChange; //For IE
            document.getElementsByTagName('head')[0].appendChild(currentScript);
        }

        
else
        
{
            currentScript 
= document.getElementById(id);
            
if(currentScript.tagName != "SCRIPT")
            
{
                alert(
"有相同id的不同类型元素存在");
                
return;
            }


            
for(var i=0; i<queuedScripts.length; i++)
            
{
                
if(queuedScripts[i].id === id)
                
{
                    obj 
= queuedScripts[i];
                    
break;
                }

                
if(obj == nullreturn;
            }

        }

        currentScript.src 
= obj.scriptPath;
        currentScript.text 
= obj.scriptText;
    }

}

function  ScriptBlock(id,scriptPath,scriptText)
{
    
if(scriptPath == null)scriptPath = "";
    
if(scriptText == null)scriptText = "";
    
this.id = id;
    
this.scr = scriptPath;
    
this.text = scriptText;
}

拙作旨在抛砖引玉..

你可能感兴趣的:(script)