ready method for jQuery

In the book, the author said the onload method of window just be triggered till the DOC tree is created and all resources (like image and other external resources) are fully loaded and the page is dispayed in the browser window. So since loading the recources takes some time, jQuery provide a better event read function which will be trigger just after the DOM tree is created not the loading the resources fully. Like following code:

$(document).ready(function() { $("table tr:nth-child(even)").addClass("even");});

After further research, I will find the ready function (this ready function is intance level, following we will introduce another ready function belongs to jQuery function which is function level) is following:

ready: function( fn ) {
  // Attach the listeners
  jQuery.bindReady();

  // Add the callback
  readyList.add( fn );

  return this;
 },

from the code we could find it just store the pass-in function in inner parameter readyList which type is jQuery.Callbacks (readyList = jQuery.Callbacks( "once memory" );). Before this it invokes the jQuery's bindReady method

bindReady: function() {
....

  // 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) {}

  ....   }
  }
 }

from the code we could figure out it attach the DOMContentLoaded method to the DOMContentLoaded or onreadystatechange event and attach jQuery.ready to load event.

Following we will introduce  DOMContentLoaded and jQuery.ready function:

if ( document.addEventListener ) {
 DOMContentLoaded = function() {
  document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
  jQuery.ready();
 };

} else if ( document.attachEvent ) {
 DOMContentLoaded = function() {
  // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
  if ( document.readyState === "complete" ) {
   document.detachEvent( "onreadystatechange", DOMContentLoaded );
   jQuery.ready();
  }
 };
}

from this code we could find the main purpose is the invoke jQuery.ready function and detach itself from relevant event.

It's turn to jQuery.ready:

// Handle when the DOM is ready
 ready: function( wait ) {
   ...

   // If there are functions bound, to execute
   readyList.fireWith( document, [ jQuery ] );

   // Trigger any bound ready events
   if ( jQuery.fn.trigger ) {
    jQuery( document ).trigger( "ready" ).off( "ready" );
   }
  }
 },

from the code we could fingure out it triggers the methods stored in the readyList and then detach itself from the relevant event in the last one.

 

In one word, for IE the ready function it uses onreadystatechange event, in the other side for other broswers it uses DOMContentLoaded event.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值