我不想在啓動時調用JavaScript函數。原因很簡單,它會減少初始下載大小,並且頁面將顯得更快。要做到這一點我用頭段內的下列代碼..如何推遲加載JavaScript文件?
<script type="text/javascript">
// Add a script element as a child of the body
function downloadJSAtOnload() {
var element = document.createElement("script");
element.src = "deferredfunctions.js";
document.body.appendChild(element);
}
// Check for browser support of event handling capability
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else window.onload = downloadJSAtOnload;
</script>
但它沒有工作......請幫我
相關:http://stackoverflow.com/questions/912711/jquery-to-load-javascript-file-dynamically – deepmax