动态添加脚本,并触发回调函数 初步实现按需加载
function loadScript(url, callback){ var script = document.createElement("script") script.type = "text/javascript"; if (script.readyState){ //IE script.onreadystatechange = function(){ if (script.readyState == "loaded" || script.readyState == "complete"){ script.onreadystatechange = null; callback(); } }; } else { //Others script.onload = function(){ callback(); }; } script.src = url; document.getElementsByTagName("head")[0].appendChild(script); }
调用方法:
loadScript("http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js", function(){ alert('load success1!'); });