检查DOM过度的准备情况? [英] Is checking for the readiness of the DOM overkill?

查看:106
本文介绍了检查DOM过度的准备情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个利用Web技术开发桌面应用的平台。在这样做的过程中,我一直在尝试通过浏览器获取一些文档/就绪功能,我将集成到该平台中。这就是为什么我以前在这里问过这个问题: javascript-framework-主要提供只是文档的功能



但是,我无法获得我的浏览器嘘,它的秘密;)成功地利用上述唯一的回答建议的功能。所以,在试图找出可能的工作的过程中,我偶然发现了以下几点。



下面的代码在这个浏览器中具有相同的效果,我在简单地使用1毫秒的超时后执行一个函数:我可以写入DOM,而大图像正在加载。这可能不是我的最终解决方案,我可能会写一些特定的DOM功能如何通过这个浏览器的Javascript引擎实现。



然而,我决定看到如果这在标准浏览器中工作,令我惊奇的是,它确实如此!有鉴于此,我的问题是:各种Javascript框架提供的各种实现的dom / readyiness功能,只是过度使用?

  ;!DOCTYPE HTML PUBLIC -  // W3C // DTD HTML 4.01 Transitional // EN
http://www.w3.org/TR/html4/loose.dtd\">
< html>
< head>
< title> Untitled Document< / title>
< meta http-equiv =Content-Typecontent =text / html; charset = iso-8859-1>
< script>
setTimeout(function(){
var txtNode = document.createTextNode(ready_yet?);
var ready_yet_el = document.getElementById(ready_yet);
ready_yet_el.appendChild (txtNode);
},1);
< / script>
< / head>

< body>
< div id =ready_yet>< / div>
< img src =http://www.ryanmorr.com/tests/ondomready/pic.jpg/>
< / body>

< / html>

编辑/进一步理解
通过我以前的相关问题的答案链接的页面它指出对于Firefox和Opera,事件类型的简单检查将确定是否它是DOMContentLoaded,Safari和IE将检查文档的就绪状态....最后,如果所有其他的失败,onload事件将提起后面。也许一个类似我上面的setTimeout的setInterval可能是倒数第二个操作过程,然后依靠onload作为最后的手段?在任何情况下,使用我选择的嵌入式浏览器,DOMContentLoaded事件和document.readyState都不被支持。

解决方案

你的预感是好的,有根据的IMO。但是有人打败你已经打了。简单的答案是, setTimeout 不是在所有情况下检测DOM准备的工作实现。可能您的浏览器感兴趣,但IE在某些情况下失败。



可能会感兴趣的是,知道Microsoft自己的ASP.NET AJAX框架使用 setTimeout 欺骗检测DOM准备就绪。令人惊讶的是,惊喜:它失败在某些用例中也是如此。简单来说,由于文件大小(例如〜500K)或网络/服务器延迟时间,这个问题似乎在IE中加载缓慢的脚本。


I'm developing a platform for developing desktop apps with web technologies. In the course of doing so I've been trying to get some document/on-ready functionality working with the browser I will be integrating into the platform. That's why I'd previously asked this this question here on SO: javascript-framework-that-primarily-provides-just-document-onready-functionality

However I've not been able to get my browser of choice (shush, its a secret ;) to successfully utilize the functionality suggested by the one and only answer to the above. So, in the course of just trying to figure out what might possibly work I stumbled upon the following.

The code below has the same effect within this browser I'm using simply by executing a function after a timeout of 1 millisecond: I can write to the DOM while the big image is loading. This might not be the ultimate solution for me, I may write something specific to how DOM functionality is implemented by the Javascript engine for this browser.

Nevertheless, I decided to see if this works in standard browsers, and much to my surprise, it does! In light of which, my question: are various implementations of dom/readiness functionality provided by various Javascript frameworks, simply overkill?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script>
setTimeout(function() {
    var txtNode = document.createTextNode("ready_yet?");
    var ready_yet_el = document.getElementById("ready_yet");
    ready_yet_el.appendChild(txtNode);
},1);
</script>
</head>

<body>
<div id="ready_yet"></div>
<img src="http://www.ryanmorr.com/tests/ondomready/pic.jpg" />
</body>

</html>

EDIT/FURTHER THOUGHTS On the page linked to by the answer to my previous related question it states "For Firefox and Opera a simple check of event type will determine if it is DOMContentLoaded. Safari and IE will check against the document’s ready state....Finally in case all else fails, the onload event will bring up the rear." Perhaps a setInterval similar to my setTimeout above could be the penultimate course of action, before relying on onload as a last resort? In any event, with the embeddable browser I've chosen, neither the DOMContentLoaded event, nor document.readyState appear to be supported.

解决方案

Your hunch is good and well founded IMO. But someone has beat you to the punch already. Short answer is that setTimeout is not a working implementation of detecting DOM readiness in all cases. It may be ok for your browser of interest, but IE fails in some situations.

It may interest you to know that Microsoft's own ASP.NET AJAX framework uses the setTimeout trick to detect DOM readiness. And surprise, surprise: it fails in certain use cases as well.

In short, the problem seems to lie in IE with slow loading scripts, either due to large file size (eg ~500K) or network/server latency.

这篇关于检查DOM过度的准备情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆