如果第三方javascript文件挂起并需要一段时间加载,将会
jQuery(document).ready(function() {})
在被叫之前必须等待加载?
是的,它必须等待.特别是,jQuery(document).ready()
在其他脚本有机会执行之前,您不能依赖于触发.
ready
绑定到DOMContentReady,readystatechanged或onload,无论哪个可用.
该文档指出"在大多数情况下,脚本可以在DOM层次结构完全构建后立即运行".请注意,唯一的保证是在此事件触发时DOM已准备就绪. 它不能保证你的任何其他东西 - 因为它不能.
这一点,例如不会在IE,Firefox或铬工作,brilliant.js是一直呼吁的前ready()
处理程序有机会执行不管你怎么洗牌脚本标记:
Test
仅供参考,这是jquery-1.4.2的相关代码:
bindReady: function() { if ( readyBound ) { return; } readyBound = true; // Catch cases where $(document).ready() is called after the // browser event has already occurred. if ( document.readyState === "complete" ) { return jQuery.ready(); } // Mozilla, Opera and webkit nightlies currently support this event if ( document.addEventListener ) { // Use the handy event callback document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false ); // A fallback to window.onload, that will always work window.addEventListener( "load", jQuery.ready, false ); // If IE event model is used } else if ( document.attachEvent ) { // ensure firing before onload, // maybe late but safe also for iframes document.attachEvent("onreadystatechange", DOMContentLoaded); // A fallback to window.onload, that will always work window.attachEvent( "onload", jQuery.ready ); // If IE and not a frame // continually check to see if the document is ready var toplevel = false; try { toplevel = window.frameElement == null; } catch(e) {} if ( document.documentElement.doScroll && toplevel ) { doScrollCheck(); } } },