Let's face it, jQuery/jQuery-ui is a heavy download.
谷歌建议使用deferred loading of JavaScript来加速初始渲染.我的页面使用jQuery设置一些选项卡,这些选项卡放在页面的较低位置(大部分在初始视图之外),我希望将jQuery推迟到页面呈现之后.
Google's deferral code adds a tag to the DOM after the page loads by hooking into the body onLoad event:
<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>
I'd like to defer loading of jQuery this way, but when I tried it my jQuery code failed to find jQuery (not completely unexpected on my part):
$(document).ready(function() {
$("#tabs").tabs();
});
所以,我似乎需要找到一种方法,将jQuery代码的执行推迟到jQuery加载之后.如何检测添加的标记已完成加载和解析?
作为推论,似乎asynchronous loading也可能包含答案.
有什么 idea 吗?