JS 异步加载的方法
(1) defer,只支持IE :
<script type="text/javascript" defer="defer"> </script>
(2) async:属性
<script type="text/javascript" src="demo_async.js" async="async"></script>
(3) 创建script,插入到DOM中,加载完毕后callBack,见代码:
function loadScript(url, callback){ var script = document.createElement_x("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: Firefox, Safari, Chrome, and Opera script.onload = function(){ callback(); }; } script.src = url; document.body.appendChild(script); }