- 浏览: 57506 次
- 性别:
- 来自: 广州
最新评论
-
Joson_Coney:
11.
while( (*strDest++ = *strSr ...
c++笔试题汇总 -
Joson_Coney:
③ 1.
int func(x)
{
int countx = ...
c++笔试题汇总 -
Joson_Coney:
链表反向1:
#include <cstdlib&g ...
c++笔试题汇总
( From: http://www.javascriptkit.com/dhtmltutors/domready.shtml)
( Crazy implementation with pure javascript. Just in order to take place of " $(document).ready(function(){...});".
......
)
Relying on DOM readiness to invoke a function (instead of window.onload)
One of the most common event web developers check for before running their JavaScript is whether the page has completely loaded. For example:
window.onload=function(){
walkmydog()
}
This technique has become very popular as a way of defining our scripts in a central location without having to worry about the availability of the page content it seeks to manipulate. The bottom line is, when the page has fully loaded, whatever we want on the page has as well, ready to be molded. However, it's not without its flaws- waiting for the entire page to load literally means that, which can include slow loading images/iframes present on the page, and isn't always necessary. That's why Dean Edwards, JavaScript extraordinaire, came up with an alternate techniqueto detect page DOM readiness instead, which just means finding out when the document's DOM tree has fully initialized, versus the entire document and every object in it. A classic example is the insertion/ deletion of nodes via the DOM, which can in fact be done sooner rather than later, when the DOM has loaded versus the document itself (window.onload
). In this tutorial, let me recap and perhaps refine a bit Dean Edward's technique for calling a function as soon as the document DOM is ready!
Checking DOM readiness in Firefox and Opera 9+
In Firefox and Mozilla, checking for DOM readiness and executing a function when it's ready is simple, as a special "DOMContentLoaded"
keyword is supported that can be used in place of an element when calling document.addEventListener()
:
if (document.addEventListener)
document.addEventListener("DOMContentLoaded", walkmydog, false)
In this case the function walkmydog()
will run as soon as the DOM for the document has fully initialized, which depending on the contents of your page can occur a lot sooner thanwindow.onload
.
Checking DOM readiness in Internet Explorer
In IE (includes IE7), there's no intrinsic way to detect DOM readiness. Dean Edwards came up with a roundabout solution instead, which relies on the behaviour of the defer attribute when added to an external JavaScript tag. In short this attribute causes IE to "defer" loading the referenced external JS file until the DOM has fully loaded, a behaviour that we can take advantage of here to accomplish what we want:
if (document.all && !window.opera){ //Crude test for IE //Define a "blank" external JavaScript tag document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>') var contentloadtag=document.getElementByIdx_x_x("contentloadtag") contentloadtag.onreadystatechange=function(){ if (this.readyState=="complete") walkmydog() } }
As you can see, this technique centers around defining a "blank" external JavaScript tag with the "defer" attribute added. In IE, most elements support a "onreadystatechange
" event that fires during each stage of the element's loading process, ending with "complete". So by probing theonreadystatechange
event of our deferred blank JavaScript tag, we can in effect call the desired function when the DOM is ready.
Putting it all together, plus a fall back plan
You just saw the two divergent techniques for detecting DOM readiness. It's time now to put the two halves together, but more importantly, implement a fall back mechanism for browsers that don't support any of the two "detect DOM readiness techniques" to default to using window.onload
instead. With that said:
var alreadyrunflag=0 //flag to indicate whether target function has already been run if (document.addEventListener) document.addEventListener("DOMContentLoaded", function(){ alreadyrunflag=1; walkmydog() }, false) else if (document.all && !window.opera){ document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>') ; var contentloadtag=document.getElementByIdx_x_x("contentloadtag"); contentloadtag.onreadystatechange=function(){ if (this.readyState=="complete"){ alreadyrunflag=1; walkmydog() } } } window.onload=function(){ setTimeout("if (!alreadyrunflag) walkmydog()", 0) }
Note the code in red- this is our fallback mechanism that shall account for the scenario in which the target function wasn't run at the DOM ready stage for any reason, and run it via the traditional window.onload
event instead. With it in place, no browser should fall through the cracks at the end of the day. Two things I should point out with this scheme that makes it work:
-
A global flag "
alreadyrunflag
" is defined to let the fallback code know whether it should run the target function viawindow.onload
. If the flag evaluates to true, it means the target function has already been run at the DOM readiness stage (which proceedswindow.onload
). -
In IE and for very basic pages that contain just a few lines of text, there is the possibility that
window.onload
could fire at the same time thereadyState
property of the external JavaScript tag evaluates to "complete
", instead of the former always following the later . In such a unique case, the flagalreadyrunflag
won't evaluate to true in time for our fall back function to know that the target function has already been run in IE, and will run it again. This problem can easily be fixed by embedding asetTimeout()
withinwindow.onload
, ensuring the code attached to this event will in fact always be the very last thing run on the page.
And there you have it- a way to launch functions as soon as the DOM is ready and not leave non capable browsers in the dust. Using the exact same technique, the below example shows launching two functions this time around, the later with a parameter:
var alreadyrunflag=0 //flag to indicate whether target function has already been run if (document.addEventListener) document.addEventListener("DOMContentLoaded", function(){alreadyrunflag=1; walkmydog(); feedcat('grapes')}, false) else if (document.all && !window.opera){ document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>') var contentloadtag=document.getElementByIdx_x_x("contentloadtag") contentloadtag.onreadystatechange=function(){ if (this.readyState=="complete"){ alreadyrunflag=1 walkmydog() feedcat('grapes') } } } window.onload=function(){ setTimeout("if (!alreadyrunflag){walkmydog(); feedcat('grapes')}", 0) }
In conclusion, and checking for DOM readiness in Safari
The advantage of running a function as soon as the DOM is ready versus window.onload
materializes on pages that are heavy on mages or iframe tags. Ultimately you have the weigh the benefits versus the additional code and work needed to implement such a technique. Before I conclude this tutorial, there's an elephant in the room I've been ignoring until now. Apparently in Safari it's also possible to check for DOM readiness, and Dean Edward referred to John Resig in coming up with a way to do so in that browser:
if(/Safari/i.test(navigator.userAgent)){ //Test for Safari var _timer=setInterval(function(){ if(/loaded|complete/.test(document.readyState)){ clearInterval(_timer) walkmydog() // call target function }}, 10) }
It's the obsessive DOM readiness checker! In Safari, apparently, what you need to do is manually probe the document.readyState
property at fast paced intervals to see when it returns either "loaded
" or "completed
" (usually the former proceeds the later, but to be safe, check both). That's when you know the DOM is ready and to fire your desired function. Personally I'm not keen on implementing it, as this is where the technique crosses the line and becomes a burden in my opinion considering the small group it benefits. And yes we're all entitled to our opinions!
发表评论
-
【转】Compiling with cython and mingw produces gcc: error: unrecognized command lin
2012-05-03 10:46 1069(From: http://stackoverflow.com ... -
Java Annotation入门
2012-04-17 15:01 0(转自:http://www.blogjava.net/eri ... -
【转】DNS协议报文(RFC1035)
2012-01-27 06:46 1339(转自:http://hi.baidu. ... -
【转】Protocol Header Images
2012-01-27 06:45 662http://www.troyjessup.com/heade ... -
【转】DNS协议及应用
2012-01-27 06:44 949(转自http://jwx.zgz.cn/cl/7 ... -
DNS 伺服器
2012-01-26 06:03 538DNS 伺服器http://linux.vbird.org/l ... -
HTTP Request
2012-01-26 06:02 766HTTP Request Published ... -
HTTP multipart/form-data 上传方式说明
2012-01-26 06:01 869( From: http://home.meegoq.c ... -
Java:对象的强、软、弱和虚引用
2012-01-09 15:00 562原创作品,允许转载,转载时请务必以超链接形式标明文 ... -
Struts2源码分析--请求处理
2011-12-01 10:03 651(转自:http://www.blogjava.ne ... -
java web学习笔记
2011-11-21 09:50 596Tomcat Configuration 资 ... -
c++笔试题汇总
2011-10-23 16:11 698(转自http://blog.csdn.net/dongfen ... -
c++中使用引用传递来提高效率
2011-10-23 14:37 910(转自:http://www.diybl.com/course ... -
[转] HTTP multipart/form-data 上传方式说明
2011-10-15 06:08 1316( From: http://home.meegoq.co ... -
Multipart uploading spec
2011-10-15 06:07 682Upload File: abc.txtContent:abc ... -
【转】Webcam Manipulation with Javascript
2011-10-15 06:04 812( From:http://blog.trumpton.org ... -
GTK+ 中文显示解决方案
2011-10-15 06:01 660(转自:http://www.wangchao.net.cn/ ... -
Table单元格td的position:relative的兼容性
2011-10-11 12:35 1922( From: http://www.itref.cn/a/ ... -
a lib: jXHR (JSON-P XHR)
2011-10-11 12:18 707http://mulletxhr.com/ -
Remote JSON - JSONP
2011-10-11 12:16 695( From : http://bob.pythonmac.o ...
相关推荐
of relying on a single “best guess” as to what might be the case in the world, probabilistic algorithms represent information by probability distributions over a whole space of possible hypotheses. ...
- Old English, with its inflectional system, was indeed highly inflected, unlike Modern English, which is more analytic, relying on word order and function words to convey meaning. - Compounds are ...
Rcpp attributes provide a high-level syntax for declaring C++ functions as callable from R and automatically generating the code required to invoke them. Attributes are intended to facilitate both ...
We introduce a network that directly predicts the 3D layout of lanes in a road scene from a single image. This work marks a first attempt to address this task with onboard sensing without assuming a ...
A database administrator tries to get the most out of a systema given hardware, processors and storage subsystem, or a given version of the database. A database administrator may have some SQL skills...
But relying solely on jQuery as your window to the web leaves large gaps in your knowledge. This in turn results in frustration when the abstraction that jQuery provides “leaks” and exposes you to ...
But relying solely on jQuery as your window to the web leaves large gaps in your knowledge. This in turn results in frustration when the abstraction that jQuery provides "leaks" and exposes you to ...
Instead of optimizations based on closed-form analysis of individual protocols, network operators need data- driven, machine-learning-based models of end-to-end and application performance based on ...
As you progress in the book, you will be relying on code from previous chapters in order to help create new solutions quickly. By the end of the book, you will be able to manipulate, find, and ...
OpenResty itself has been relying on automated testing to remain high quality over the years. As OpenResty core developers, we embrace the test driven development (TDD) process all the time. An ...
we demonstrate the application of convolutional neural networks on raw IQ samples for mod- ulation classification which achieves competitive accuracy with respect to traditional schemes relying on ...
When we created our Website Builder, we decided to start from scratch instead of relying on what already existed. This approach gave us the freedom to focus on the things that are really important ...
Its ability to extract features from a large set of raw data without relying on prior knowledge of predictors makes deep learning potentially attractive for stock market prediction at high ...
This thesis presents the design and implementation of a book management system based on Microsoft Foundation Classes (MFC), aiming to address these challenges and cater to the general management ...
Similarly, many judicial systems, whether relying on a jury or a panel of judges, operate on this same basis. Ensemble learning can be understood as a technique that combines multiple weak learners ...
A multiple gridding strategy is applied to capture the distinct patterns of the patch at different spatial granularities, leading to a high distinctiveness of LDB. LDB is very suitable for vision ...
The overemphasis on exams can lead to a narrow focus on memorization and test-taking skills rather than fostering critical thinking, creativity, and problem-solving abilities. To build a successful ...