Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the jQuery $(window).load(); event not fire on pages without a <!DOCTYPE> declaration? (...in a chrome extension content script)

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>

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.

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

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

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

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?

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

like image 499
theMaxx Avatar asked Nov 05 '12 12:11

theMaxx


1 Answers

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

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:

  • 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/

like image 56
LukeGT Avatar answered Nov 11 '22 07:11

LukeGT