jQuery $(window).load();没有<!DOCTYPE>的网页上的事件不会触发宣言? (...在chrome扩展内容脚本中) [英] Does the jQuery $(window).load(); event not fire on pages without a <!DOCTYPE> declaration? (...in a chrome extension content script)

查看:117
本文介绍了jQuery $(window).load();没有<!DOCTYPE>的网页上的事件不会触发宣言? (...在chrome扩展内容脚本中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个Google Chrome浏览器扩展程序来处理网页,但在部分加载(DOM)或完全加载(使用图像)之后。

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).

现在看来,很多网站使用

It seems that many sites nowadays use the

<!DOCTYPE html>

声明,或其中的一些变体,但其他许多则没有。问题主要是关于HTML doctypes ...我不知道其他的。

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.

假设如果一个网页没有DOCTYPE声明是安全的,那么$(window).load();不会被解雇?

Is it safe to assume that if a webpage does not have the DOCTYPE declaration, then $(window).load(); will not be fired?

最初我使用$(document).ready(); (当DOM加载时),但后来切换到$(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).

事情是,现在$(window).load();如果没有DOCTYPE,似乎不起作用。 $(文件)。就绪();似乎可以在所有页面上工作,无论是否声明DOCTYPE。

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.

也许这对于同样问题的其他人来说可能是有用的。我搜了一下,没有找到决定性的答案。看来我最终会使用这样的东西:

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);}

我猜我的问题是...是否需要检查DOCTYPE才能知道要使用哪个事件?或者我在这里缺少一些东西?

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?

基本上,为什么$(window).load();如果没有DOCTYPE声明,似乎不会触发?

Basically, why does $(window).load(); seem not to fire if there's no DOCTYPE declaration?

推荐答案

基本上,你不应该使用$(window).load ),因为它不完全支持。如果你真的需要它,那么上面的解决方案是你能做的最好的。 jQuery页面很好地总结了这些注意事项:

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:


使用图像时加载事件的注意事项

Caveats of the load event when used with images

开发人员尝试使用.load()
快捷方式解决的常见问题是在图像(或
图像的集合)已完全加载时执行功能。有几个已知的警告,
这个应该注意。这些是:

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:


  • 它的工作不一致也不可靠地跨浏览器

  • 如果图像src设置为与之前相同的src,则在WebKit中正常启动

  • 它没有正确地起泡DOM树

  • 可以对于已经在浏览器缓存中的图像,停止触发

  • 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
  • It doesn't correctly bubble up the DOM tree
  • Can cease to fire for images that already live in the browser's cache

URL: http://api.jquery.com/load-event/

这篇关于jQuery $(window).load();没有&lt;!DOCTYPE&gt;的网页上的事件不会触发宣言? (...在chrome扩展内容脚本中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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