Code:loadScript( )加载js的功能函数_Javascript教程-查字典教程网
Code:loadScript( )加载js的功能函数
Code:loadScript( )加载js的功能函数
发布时间:2016-12-30 来源:查字典编辑
摘要:复制代码代码如下:/***functionloadScript*Copyright(C)2006DaoGottwald**Thislibra...

复制代码 代码如下:

<scripttype="text/javascript">

/**

*functionloadScript

*Copyright(C)2006DaoGottwald

*

*Thislibraryisfreesoftware;youcanredistributeitand/or

*modifyitunderthetermsoftheGNULesserGeneralPublic

*LicenseaspublishedbytheFreeSoftwareFoundation;either

*version2.1oftheLicense,or(atyouroption)anylaterversion.

*

*Thislibraryisdistributedinthehopethatitwillbeuseful,

*butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof

*MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.SeetheGNU

*LesserGeneralPublicLicenseformoredetails.

*

*YoushouldhavereceivedacopyoftheGNULesserGeneralPublic

*Licensealongwiththislibrary;ifnot,writetotheFreeSoftware

*Foundation,Inc.,51FranklinStreet,FifthFloor,Boston,MA02110-1301USA

*

*Contactinformation:

*DaoGottwald<daoatdesign-noir.de>

*Herltestra?e12

*D-01307,Germany

*

*@version1.5

*@urlhttp://design-noir.de/webdev/JS/loadScript/

*/

functionloadScript(url,callback){

varscript=document.createElement('script');

script.type='text/javascript';

/*shouldbeapplication/javascript

*http://www.rfc-editor.org/rfc/rfc4329.txt

*http://connect.microsoft.com/IE/feedback/ViewFeedback.aspx?FeedbackID=84613

*/

if(callback)

script.onload=script.onreadystatechange=function(){

if(script.readyState&&script.readyState!='loaded'&&script.readyState!='complete')

return;

script.onreadystatechange=script.onload=null;

callback();

};

script.src=url;

document.getElementsByTagName('head')[0].appendChild(script);

}

</script>

实例:

复制代码 代码如下:<scripttype="text/javascript">

//preventgoogleanalyticsfromslowingdownpageloading

window.addEventListener('load',function(){

loadScript('http://www.google-analytics.com/urchin.js',function(){

window._uacct='UA-xxxxxx-x';

urchinTracker();

});

},false);

</script>

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新Javascript教程学习
    热门Javascript教程学习
    编程开发子分类