window.document是否为空或未定义? [英] Is window.document ever null or undefined?

查看:308
本文介绍了window.document是否为空或未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



为了讨论起见,这里是一个非相关的示例代码。有没有什么情况下这段代码会失败(也就是抛出异常)?

  $(document).ready (function(){
var PageLoaded =(window.document.readyState ===complete);
});


解决方案


有没有window.document对象为空或未定义的情况?


是的,对于不在文档中的JavaScript代码(例如节点.js文件)。但是,这样的代码可能没有一个窗口对象(尽管它将有一个全局对象)。



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

 >是否有任何情况下这段代码将失败(即抛出
>异常)?
>
> [snip jQuery代码]

它会失败在哪里:


$ b $ jQuery ready功能失败(可能在至少一些浏览器中,但不是桌面设备和某些移动设备的流行应用程序),
  • 没有窗口对象,或

  • 没有 window.document 对象

  • 为了确保在各种主机中工作的代码,您可以执行以下操作:

      if(typeof window!='undefined'&&&&&&                    
    //做东西
    }

    这不是额外的写,可能只需要完成一次。



    替代方案:

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

     (function(global){
    var window = global;

    function checkState(){
    if(window.document&& window.document.readyState ){
    alert(window.document.readyState);
    } else {
    //分析环境
    }
    }
    //简单的使用演示
    checkState();
    setTimeout(checkState,1000);
    }(this));


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

    解决方案

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

    这篇关于window.document是否为空或未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆