欢迎来到福编程网,本站提供各种互联网专业知识!

Code:loadScript( )加载js的功能函数

发布时间:2007-02-02 作者: 来源:转载
复制代码代码如下:/***functionloadScript*Copyright(C)2006DaoGottwald**Thislibraryisfreesoftware;youcanredistributeitand/or*modifyitunderthetermsoftheGNULesserGeneralPublic*LicenseaspublishedbytheFreeSoftwareFoundation;either*version2.1of
复制代码 代码如下:

/**
*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
*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);
}

实例:
复制代码 代码如下:
//preventgoogleanalyticsfromslowingdownpageloading
window.addEventListener('load',function(){
loadScript('http://www.google-analytics.com/urchin.js',function(){
window._uacct='UA-xxxxxx-x';
urchinTracker();
});
},false);

相关推荐