javascript window.document 是否为空或未定义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12118971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:21:55  来源:igfitidea点击:

Is window.document ever null or undefined?

javascriptdom

提问by N. Taylor Mullen

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?

我一直在对 window.document 对象进行一些研究,以确保我的 JavaScript 解决方案之一是可靠的。当 window.document 对象为空或未定义时,是否有过这种情况?

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

采纳答案by RobG

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

当 window.document 对象为空或未定义时,是否有过这种情况?

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

是的,对于不在文档中的 JavaScript 代码(例如 node.js)。但是这样的代码也可能没有 window 对象(尽管它会有一个全局对象)。

For code in HTML documents in user agents compliant with the W3C DOM, no.

对于符合 W3C DOM 的用户代理中 HTML 文档中的代码,没有。

> 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.documentobject
  1. jQuery 就绪功能失败(至少在某些浏览器中可能会失败,尽管不是在桌面和某些移动设备中流行使用的浏览器),
  2. 没有window object,或者
  3. 没有window.document对象

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

回答by user907860

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

我认为文档总是被定义的,因为浏览器显示的只是一个 html 文档,甚至site is not available. 更多,document是只读属性

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

回答by Jeremy J Starcher

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

忽略 Javascript 在网络浏览器/用户代理之外的其他地方运行的事实,您的 pageLoaded 测试可能会在 iframe 上失败(未经测试,但我知道它们变得很奇怪)。

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.

可能还有一些关于“页面加载”是什么意思的问题。您是否正在尝试查看 DOM 是否已呈现并且元素是否已准备好进行操作?或者您是否正在检查页面加载是否确实完成,其中包括加载所有其他元素,例如图形。

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

这个讨论可能有用: 如何在没有框架的情况下检查 DOM 是否准备就绪?

回答by FloatFish

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.

因为你的javascript代码必须写在html文档中,所以你的代码不能在文档之外执行,换句话说,没有文档,没有javascript。