动态加载js

http://www.360doc.com/content/10/0225/16/284485_16794533.shtml

4种动态加载的方法

 

http://topic.csdn.net/u/20090902/18/52b3fcb5-26a6-4ee3-b2df-2a7d86002aaa.html

两种效率的比较

 

原来,系统优化不是什么高深的技术,自己开发中总结的小经验,综合起来就是优化的好办法!

 

//异步加载 function loadJS(id,fileUrl) { var scriptTag = document.getElementById(id); var oHead = document.getElementsByTagName('HEAD').item(0); var oScript= document.createElement("script"); //alert(scriptTag); if (scriptTag){alert("已经存在"); return; //oHead.removeChild(scriptTag); } //alert("通过"); oScript.id = id; oScript.type = "text/javascript"; oScript.src=fileUrl; oHead.appendChild(oScript); } //使用extajax方法同步加载 function loadJSByExtAjax(id,fileUrl,successFUN){ if(document.getElementById(id)){ alert("js已经存在!"); } Ext.Ajax.request({ url: fileUrl, success: function(data){ //alert(data.responseText); if((data != null)&& (!document.getElementById(id))){ var oHead = document.getElementsByTagName('HEAD').item(0); var oScript = document.createElement( "script" ); oScript.language = "javascript"; oScript.type = "text/javascript"; oScript.id = id; oScript.defer = true; oScript.text = data.responseText; oHead.appendChild(oScript); } successFUN(); }, failure: function(){alert("服务器连接失败!请检查网络状况!");}, headers: { 'my-header': 'foo' }, params: {} }); } Ext.onReady(function(){ new Ext.Button({ renderTo:Ext.getBody(), text:'动态加载JS文件', handler:function(){ var successFUN = function(){ alert(personName); } alert(); loadJSByExtAjax("loadTest","mbca/test/loadTest.js",successFUN); } }); }); //这两种方法貌似都有缓存,因为我改动加载的loadTest.js之后,加载过来的js代码都不变啊,只有把浏览器关了才会加载修改过的代码。  

你可能感兴趣的:(动态加载js)