I’ve been doing some research on the window.document object in order to make sure one of my JavaScript solutions is reliable. Is there ever a case when the window.document object is null or undefined?
For the sake of discussion here’s a non-relevant example piece of code. Are there any situations in which this piece of code will fail (aka, throw an exception)?
$(document).ready(function() {
var PageLoaded = (window.document.readyState === "complete");
});
Yes, for JavaScript code that isn’t in a document (e.g. node.js). But such code may not have a window object either (though it will have a global object).
For code in HTML documents in user agents compliant with the W3C DOM, no.
It will fail where:
window object, orwindow.documentobjectTo be confident of code working in a variety of hosts, you can do something like:
which isn’t much extra to write, and likely only needs to be done once.
Alternatives:
and