I'm working on a Google Chrome extension that manipulates a webpage, but after it is either partially loaded (the DOM) or fully loaded (with images).
我正在开发一个操纵网页的Google Chrome扩展程序,但是在部分加载(DOM)或完全加载(带图像)之后。
It seems that many sites nowadays use the
现在似乎很多网站都使用了
<!DOCTYPE html>
declaration, or some variation of it, but many others do not. The question is mainly about HTML doctypes...I'm not sure about the others.
声明,或其中的一些变体,但许多其他人没有。问题主要是关于HTML doctypes ...我不确定其他人。
Is it safe to assume that if a webpage does not have the DOCTYPE declaration, then $(window).load(); will not be fired?
假设如果网页没有DOCTYPE声明,则可以安全地使用$(window).load();不会被解雇?
In the beginning I was using $(document).ready(); (for when the DOM is loaded), but later switched to $(window).load(); (to let the images load too).
一开始我使用$(document).ready(); (对于加载DOM时),但后来切换到$(window).load(); (也让图像加载)。
The thing is, now $(window).load(); does not seem to work if there is no DOCTYPE. $(document).ready(); seems to work on all pages, regardless of whether a DOCTYPE is declared or not.
问题是,现在$(window).load();如果没有DOCTYPE似乎不起作用。 $(文件)。就绪();无论是否声明DOCTYPE,似乎都适用于所有页面。
Maybe this can be useful for others with this same issue. I searched a bit and didn't find a decisive answer. It seems that I will end up using something like this:
也许这对于有同样问题的其他人有用。我搜索了一下,没有找到决定性的答案。看来我最终会使用这样的东西:
if (window.document.doctype != null) {$(window).load(checkEntries);}
if (window.document.doctype == null) {$(document).ready(checkEntries);}
I guess my question is... Is this normal to have to check for the DOCTYPE to know which event to use? Or am I missing something here?
我想我的问题是......这是正常的,必须检查DOCTYPE以了解使用哪个事件?或者我在这里遗漏了什么?
Basically, why does $(window).load(); seem not to fire if there's no DOCTYPE declaration?
基本上,为什么$(window).load();如果没有DOCTYPE声明,似乎不会开火?
2 个解决方案
#1
6
Basically, you shouldn't be using $(window).load(), since it's not fully supported. If you really need it, then your solution above is the best you can do. The jQuery page sums up the caveats nicely:
基本上,你不应该使用$(window).load(),因为它不是完全支持的。如果你确实需要它,那么你上面的解决方案是你能做的最好的。 jQuery页面很好地总结了这些警告:
Caveats of the load event when used with images
与图像一起使用时加载事件的注意事项
A common challenge developers attempt to solve using the .load() shortcut is to execute a function when an image (or collection of images) have completely loaded. There are several known caveats with this that should be noted. These are:
开发人员尝试使用.load()快捷方式解决的常见挑战是在图像(或图像集合)完全加载时执行函数。应该注意有几个已知的警告。这些是:
- It doesn't work consistently nor reliably cross-browser
- 它不能一致地工作,也不能可靠地跨浏览器
- It doesn't fire correctly in WebKit if the image src is set to the same src as before
- 如果图像src设置为与之前相同的src,则它在WebKit中无法正确触发
- It doesn't correctly bubble up the DOM tree
- 它没有正确地冒泡DOM树
- Can cease to fire for images that already live in the browser's cache
- 可以停止为已经存在于浏览器缓存中的图像触发
URL: http://api.jquery.com/load-event/
网址:http://api.jquery.com/load-event/
#2
3
The .ready()
method is generally incompatible with the <body onload="">
attribute. If load must be used, either do not use .ready() or use jQuery's .load()
method to attach load event handlers to the window or to more specific items, like images.
.ready()方法通常与属性不兼容。如果必须使用load,则不要使用.ready()或使用jQuery的.load()方法将加载事件处理程序附加到窗口或更具体的项目(如图像)。
#1
6
Basically, you shouldn't be using $(window).load(), since it's not fully supported. If you really need it, then your solution above is the best you can do. The jQuery page sums up the caveats nicely:
基本上,你不应该使用$(window).load(),因为它不是完全支持的。如果你确实需要它,那么你上面的解决方案是你能做的最好的。 jQuery页面很好地总结了这些警告:
Caveats of the load event when used with images
与图像一起使用时加载事件的注意事项
A common challenge developers attempt to solve using the .load() shortcut is to execute a function when an image (or collection of images) have completely loaded. There are several known caveats with this that should be noted. These are:
开发人员尝试使用.load()快捷方式解决的常见挑战是在图像(或图像集合)完全加载时执行函数。应该注意有几个已知的警告。这些是:
- It doesn't work consistently nor reliably cross-browser
- 它不能一致地工作,也不能可靠地跨浏览器
- It doesn't fire correctly in WebKit if the image src is set to the same src as before
- 如果图像src设置为与之前相同的src,则它在WebKit中无法正确触发
- It doesn't correctly bubble up the DOM tree
- 它没有正确地冒泡DOM树
- Can cease to fire for images that already live in the browser's cache
- 可以停止为已经存在于浏览器缓存中的图像触发
URL: http://api.jquery.com/load-event/
网址:http://api.jquery.com/load-event/
#2
3
The .ready()
method is generally incompatible with the <body onload="">
attribute. If load must be used, either do not use .ready() or use jQuery's .load()
method to attach load event handlers to the window or to more specific items, like images.
.ready()方法通常与属性不兼容。如果必须使用load,则不要使用.ready()或使用jQuery的.load()方法将加载事件处理程序附加到窗口或更具体的项目(如图像)。