首页 > 代码库 > jquery的isReady方法(DOM是否加载完)
jquery的isReady方法(DOM是否加载完)
1 DOMContentLoaded = function() { 2 if ( document.addEventListener ) { 3 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false ); 4 jQuery.ready(); 5 } else if ( document.readyState === "complete" ) { 6 // we‘re here because readyState === "complete" in oldIE 7 // which is good enough for us to call the dom ready! 8 document.detachEvent( "onreadystatechange", DOMContentLoaded ); 9 jQuery.ready(); 10 } 11 }, 12 13 14 ready: function( fn ) { 15 // Add the callback 16 jQuery.ready.promise().done( fn ); 17 18 return this; 19 }, 20 21 22 jQuery.ready.promise = function( obj ) { 23 if ( !readyList ) { 24 25 readyList = jQuery.Deferred(); 26 27 // Catch cases where $(document).ready() is called after the browser event has already occurred. 28 // we once tried to use readyState "interactive" here, but it caused issues like the one 29 // discovered by ChrisS here: http://bugs.jquery.com/ticket/12282#comment:15 30 if ( document.readyState === "complete" ) { 31 // Handle it asynchronously to allow scripts the opportunity to delay ready 32 setTimeout( jQuery.ready, 1 ); 33 34 // Standards-based browsers support DOMContentLoaded 35 } else if ( document.addEventListener ) { 36 // Use the handy event callback 37 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false ); 38 39 // A fallback to window.onload, that will always work 40 window.addEventListener( "load", jQuery.ready, false ); 41 42 // If IE event model is used 43 } else { 44 // Ensure firing before onl oad, maybe late but safe also for iframes 45 document.attachEvent( "onreadystatechange", DOMContentLoaded ); 46 47 // A fallback to window.onload, that will always work 48 window.attachEvent( "onload", jQuery.ready ); 49 50 // If IE and not a frame 51 // continually check to see if the document is ready 52 var top = false; 53 54 try { 55 top = window.frameElement == null && document.documentElement; 56 } catch(e) {} 57 58 if ( top && top.doScroll ) { 59 (function doScrollCheck() { 60 if ( !jQuery.isReady ) { 61 62 try { 63 // Use the trick by Diego Perini 64 // http://javascript.nwbox.com/IEContentLoaded/ 65 top.doScroll("left"); 66 } catch(e) { 67 return setTimeout( doScrollCheck, 50 ); 68 } 69 70 // and execute any waiting functions 71 jQuery.ready(); 72 } 73 })(); 74 } 75 } 76 } 77 return readyList.promise( obj ); 78 }; 79 80 81 ready: function( wait ) { 82 83 // Abort if there are pending holds or we‘re already ready 84 if ( wait === true ? --jQuery.readyWait : jQuery.isReady ) { 85 return; 86 } 87 88 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443). 89 if ( !document.body ) { 90 return setTimeout( jQuery.ready, 1 ); 91 } 92 93 // Remember that the DOM is ready 94 jQuery.isReady = true; 95 96 // If a normal DOM Ready event fired, decrement, and wait if need be 97 if ( wait !== true && --jQuery.readyWait > 0 ) { 98 return; 99 }100 101 // If there are functions bound, to execute102 readyList.resolveWith( document, [ jQuery ] );103 104 // Trigger any bound ready events105 if ( jQuery.fn.trigger ) {106 jQuery( document ).trigger("ready").off("ready");107 }108 },
jquery的isReady方法(DOM是否加载完)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。