N. Taylor Mullen
N. Taylor Mullen

Reputation: 18301

Is window.document ever null or undefined?

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

Upvotes: 9

Views: 25644

Answers (4)

RobG
RobG

Reputation: 147453

Is there ever a case when the window.document object is null or undefined?

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.

> Are there any situations in which this piece of code will fail (i.e. throw
> an exception)?
> 
> [snip jQuery code]

It will fail where:

  1. the jQuery ready function fails (likely in at least some browsers, though not the ones in popular use for desktop and some mobile devices),
  2. there is no window object, or
  3. there is no window.document object

To be confident of code working in a variety of hosts, you can do something like:

  if (typeof window != 'undefined' && window.document &&
      window.document.readyState == whatever) {
      // do stuff
  }

which isn't much extra to write, and likely only needs to be done once.

Alternatives:

(function (global) {
  var window = global;
  if (window.document && window.document.readyState == whatever) {
    // do stuff
  }
}(this));

and

(function (global) {
  var window = global;

  function checkState() {
    if (window.document && window.document.readyState) {
      alert(window.document.readyState);
    } else {
      // analyse environment 
    }
  }
  // trivial use for demonstration
  checkState();
  setTimeout(checkState, 1000);
}(this));

Upvotes: 5

FloatFish
FloatFish

Reputation: 25

Because your javascript code must be written in a html document,so your code coudn't be executed out of document,in other word,no document,no javascript.

Upvotes: 0

Jeremy J Starcher
Jeremy J Starcher

Reputation: 23863

Ignoring the fact that Javascript runs other places besides web browsers/user-agents, your pageLoaded test may fail on iframes (untested, but I know they get weird).

There may also be some question about what does "page loaded" mean. Are you trying to see if the DOM has been rendered and the elements are ready to be manipulated? Or are you checking to see if the page load is indeed complete, which includes having all of the other elements, such as graphics, loaded as well.

This discussion may be useful: How to check if DOM is ready without a framework?

Upvotes: 1

d.k
d.k

Reputation: 4470

I think document is always defined, cause all that browser shows you is a html-document, even site is not available . More, document is readonly property

window.document = null; 
console.log(window.document); //Document some.html#

Upvotes: 1

Related Questions