8

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");
});
d.k
  • 4,234
  • 2
  • 26
  • 38
N. Taylor Mullen
  • 18,061
  • 6
  • 49
  • 72
  • 1
    Are you asking to decide whether you need to check it to see? I'd hate to think about having to write "if(window && window.document && window.document. ...) {" everywhere. – dkretz Aug 25 '12 at 03:58
  • Absolutely! It'd be nice to just expect it to be defined. – N. Taylor Mullen Aug 25 '12 at 04:01
  • You should know enough about the context where you are using the document object to know whether it could be null, and how to handle it appropriately. Your validation has to be conditioned by the context. Otherwise you'd always test every variable for null each time it's used. We don't write code that way. In your example the context of testing for ready state wouldn't make sense if there weren't a real window being referred to. – dkretz Aug 26 '12 at 00:56

4 Answers4

4

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));
Lucky
  • 16,787
  • 19
  • 117
  • 151
RobG
  • 142,382
  • 31
  • 172
  • 209
  • To get global the preferred way is to use `(1, eval)("this")` instead of only `this`. – Omar Tariq Mar 14 '17 at 18:56
  • @OmarTariq—preferred by who? If *this* in the global space does not reference the global object, then the implementation is not consistent with ECMA-262 and all bets are off. In that case, what is the point of faking an indirect call to *eval*? Have you, or anyone, identified an implementation where global *this* is not the global object but `eval('this')` is? – RobG Mar 14 '17 at 23:19
1

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#
d.k
  • 4,234
  • 2
  • 26
  • 38
1

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?

Community
  • 1
  • 1
Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
  • In essence the end goal of what I'm doing is finding a reliable way to find out if a page is loaded. Basically finding a solution that works if the page is loaded immediately (detect via onload) or loaded dynamically (you miss the onload event). – N. Taylor Mullen Aug 25 '12 at 04:04
  • @N. Taylor Mullen I think that it is the reliable way, cause afaik this was defined in `DOM level 1`. Only may be on some ancient mobile browsers it is't – d.k Aug 25 '12 at 04:08
  • You'd be suprised =). In IE8 and below and all versions of Opera the PageLoaded would be true in my example even though that's only firing after the DOM has been loaded ^^. – N. Taylor Mullen Aug 25 '12 at 04:11
  • @N. Taylor Mullen, thanks for notion, but then please edit your question cause it is about `window.document` `undefined` and you need reliability of the `readyState` – d.k Aug 25 '12 at 04:17
  • The readystate itself depends directly on the windows.document object. I've already verified the reliability of the readyState object so I'm now on the pursuit to verify the windows.document object. – N. Taylor Mullen Aug 25 '12 at 04:44
  • But you don't have a readyState without a document object and a window object; so you can't say it's any more reliable than they are. If this is code to be run in a browser (outside which the test makes no sense) then there is a window. – dkretz Aug 26 '12 at 01:01
0

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.

FloatFish
  • 25
  • 2
  • 4
    Javascript doesn't have to be in an HTML document, host environments aren't restricted to documents or even browsers. – RobG Aug 25 '12 at 13:27
  • But if it weren't in an HTML document then testing document.readyState would be nonsensical and you'd have a lot bigger problems than having the window be null. – dkretz Aug 26 '12 at 00:58
  • The questions was *is there **ever** a case*. There is nothing to say that a user agent **must** provide window or document objects, and authors of web pages have no idea what user agents may access their pages. It seems sensible to test for support and not run code that will fail rather than take no precautions and just let it fail. – RobG Aug 26 '12 at 13:07
  • One very common counter-example is NodeJS – John Haugeland Aug 13 '14 at 22:45