jquery的ready事件的实现机制浅析
来源:互联网 发布:软件总工程师简历 编辑:程序博客网 时间:2024/06/04 18:39
页面初始化中,用的较多的就是$(document).ready(function(){//代码}); 或 $(window).load(function(){//代码});
他们的区别就是,ready是在DOM的结构加载完后就触发,load是在页面内包括DOM结构,css,js,图片等都加载完成后再触发,显然ready更适合作为页面初始化使用。但有时候也不尽然。需要进一步查看其内部机制。
那么ready的内部是如何判断DOM的结构加载完的?并且不同的浏览器的判断是如何的?
答案就在jquery代码内,假设jquery的版本是jquery-1.11.3.js。
ready的关键代码(3507~3566行),关键代码用红色标出:
jQuery.ready.promise = function( obj ) { if ( !readyList ) { readyList = jQuery.Deferred(); // Catch cases where $(document).ready() is called after the browser event has already occurred. // we once tried to use readyState "interactive" here, but it caused issues like the one // discovered by ChrisS here: http://bugs.jquery.com/ticket/12282#comment:15 if ( document.readyState === "complete" ) { // Handle it asynchronously to allow scripts the opportunity to delay ready setTimeout( jQuery.ready ); // Standards-based browsers support DOMContentLoaded } else if ( document.addEventListener ) { // Use the handy event callback document.addEventListener( "DOMContentLoaded", completed, false ); // A fallback to window.onload, that will always work window.addEventListener( "load", completed, false ); // If IE event model is used } else { // Ensure firing before onload, maybe late but safe also for iframes document.attachEvent( "onreadystatechange", completed ); // A fallback to window.onload, that will always work window.attachEvent( "onload", completed ); // If IE and not a frame // continually check to see if the document is ready var top = false; try { top = window.frameElement == null && document.documentElement; } catch(e) {} if ( top && top.doScroll ) { (function doScrollCheck() { if ( !jQuery.isReady ) { try { // Use the trick by Diego Perini // http://javascript.nwbox.com/IEContentLoaded/ top.doScroll("left"); } catch(e) { return setTimeout( doScrollCheck, 50 ); } // detach all dom ready events detach(); // and execute any waiting functions jQuery.ready(); } })(); } } } return readyList.promise( obj );};
上面的代码在触发ready时可以分成两部分
1.标准浏览器下的触发
当浏览器是基于标准浏览器时,会在加载完DOM结构后触发“DOMContentLoaded”事件,jquery内部就用此事件作为ready的触发源。
document.addEventListener( "DOMContentLoaded", completed, false );
2.IE浏览器下的触发
当浏览器是IE浏览器时,因为IE浏览器(蛋疼并强大着)不支持“DOMContentLoaded”事件,所以只能另谋它法,
(function doScrollCheck() { if ( !jQuery.isReady ) { try { // Use the trick by Diego Perini // http://javascript.nwbox.com/IEContentLoaded/ top.doScroll("left"); } catch(e) { return setTimeout( doScrollCheck, 50 ); } // detach all dom ready events detach(); // and execute any waiting functions jQuery.ready(); } })();
IE下的做法 就是上面代码的红字处,用“document.documentElement.doScroll("left")”的方法去滚动页面,如果没加载完就等个50毫秒后继续滚,直到滚的动后就触发ready。
但是,如果页面中有frame的场合,会使用window.onload事件作为ready的触发源。
所以在IE下,页面中有frame时,ready也是等到页面内的所有内容加载完成后再触发。
0 0
- jquery的ready事件的实现机制浅析
- $.ready()事件的实现机制
- jQuery入门:jQuery的ready事件
- 如何控制jquery的ready事件
- JS原生方法实现jQuery的ready()
- 原生js实现jQuery的ready方法
- jquery中$(document).ready(function(){})的实现
- JS原生方法实现jQuery的ready()
- jquery ready()的几种实现方法
- 浅析Qt的事件机制
- jQuery中document的ready和load事件的区别?
- jquery经典的.ready
- 学习JQuery的$.Ready()与OnLoad事件比较
- jQuery内ready与load事件的区别
- jQuery内ready与load事件的区别
- jQuery内ready与load事件的区别
- jquery中的ready事件和window.onload的区别
- jQuery内ready与load事件的区别
- Fragment无法接收从service发过来的广播
- IOS创建二维码
- 大数阶乘
- Android网络数据请求之HttpClient
- 驱动程序与应用程序之间共享内存
- jquery的ready事件的实现机制浅析
- 系统exception,以及自定义例外
- data URI scheme(图片直接保存在html页面的方法)
- [Linux] awk命令笔记 入门
- 苹果绿-护眼-色值
- (java)leetcode Lowest Common Ancestor of a Binary Search Tree
- 通常高级程序设计语言包含的语法内容
- 反调试技术揭秘
- oracle单行函数