【问题标题】:How do I get the Window object from the Document object?如何从 Document 对象中获取 Window 对象?
【发布时间】:2010-11-23 05:15:50
【问题描述】:

我可以得到window.document,但我怎样才能得到document.window?我需要知道如何在所有浏览器中执行此操作。

【问题讨论】:

  • 我敢问为什么吗? window 是一个始终可用的对象,并且一个窗口中始终不超过 1 个文档,可以说是 1-1 关系...
  • 因为我在多个 iframe 中使用原型,并且如果元素以某种方式在错误的范围内扩展,则各种 Element 方法会在 IE 或 Safari 中中断。我已经修改了一些东西来解决原型中的这个问题,但部分修复需要我找到元素所在的窗口。
  • @Joren,请考虑更改为已接受的答案。

标签: javascript html document


【解决方案1】:

The Window object is the top level object in the JavaScript hierarchy,所以简称为window

编辑: Promote JS努力之前的原始答案。 Mozilla 开发者网络上的JavaScript technologies overview 说:

在浏览器环境中,这个全局对象就是窗口对象。

编辑 2: 在阅读了作者对他的问题的评论(并获得了反对票)之后,这似乎与 iframe 的文档窗口有关。看看window.parentwindow.top,也许可以比较它们来推断您的文档窗口。

if (window.parent != window.top) {
  // we're deeper than one down
}

【讨论】:

  • open() 之后,新窗口的window 是另一个window,而不是opener。如果您从某个函数收到document(或任何Node),您可能需要原始窗口。 (哇,这很老了。)
【解决方案2】:

如果您确定 document.defaultView 是一个窗口并且可以跳过 IE 9 之前的 Microsoft 浏览器,则可以使用它。

【讨论】:

  • 是的。请记住,根据规范,document.defaultView 不必是与 window 相同的对象(事实上,在一些旧客户端中它是不同的,例如:Safari 2.x)
  • 也适用于 Firefox 中的 XUL 文档!谢谢:)
  • doc.parentWindow || doc.defaultView,它可以在旧版 IE 和 Safari 中使用
  • 不知何故我认为这应该是公认的答案?我看到了很多 javascript S.O.遵循这种格式的问题......这让我相信,当问题最初发布时,defaultView 可能不存在或没有被广泛实施......只是希望有办法用该信息标记答案...... . 我的意思是我经常看到这个。可能 Stack Overflow 的做法是罪魁祸首,所以似乎认为任何问题都是重复的问题,如果它提出相同/相似的问题。我不同意...肯定是相同/相似的措辞,但接受的答案通常是“过时的”
【解决方案3】:

嗯,这就是我采用的解决方案。它有效,但我讨厌它。

getScope : function(element) {
    var iframes = top.$$('iframe');
    var iframe = iframes.find(function(element, i) {
        return top[i.id] ? top[i.id].document == element.ownerDocument : false;
    }.bind(this, element));
    return iframe ? top[iframe.id] : top;
}   

【讨论】:

  • 只有 IE 是什么?似乎在 FF\Saf\IE\Chrome 中工作正常。但是,除了 IE 之外,它没有必要,因为其他浏览器都会在正确的范围内自动扩展元素。
【解决方案4】:

跨浏览器的解决方案很复杂,这里是 dojo 的做法(来自 window.js::get()):

// In some IE versions (at least 6.0), document.parentWindow does not return a
// reference to the real window object (maybe a copy), so we must fix it as well
// We use IE specific execScript to attach the real window reference to
// document._parentWindow for later use
if(has("ie") && window !== document.parentWindow){
    /*
    In IE 6, only the variable "window" can be used to connect events (others
    may be only copies).
    */
    doc.parentWindow.execScript("document._parentWindow = window;", "Javascript");
    //to prevent memory leak, unset it after use
    //another possibility is to add an onUnload handler which seems overkill to me (liucougar)
    var win = doc._parentWindow;
    doc._parentWindow = null;
    return win; //  Window
}

return doc.parentWindow || doc.defaultView; //  Window

has("ie") 为 IE 返回 true(否则为 false)

【讨论】:

    【解决方案5】:

    首先让我们明确一点。当您使用框架、iframe 和多个窗口时,这种事情通常是必要的,因此如果您只能处理来自 another 的文档,那么“窗口只是全局对象”是一个不令人满意的答案窗口 比你所在的那个。

    其次,不幸的是,没有直接方法可以获取窗口对象。有间接的方法。

    主要使用的机制是window.name。从某个父窗口创建窗口或框架时,通常可以给它一个唯一的名称。该窗口内的任何脚本都可以访问 window.name。窗口外的任何脚本都可以获取其所有子窗口的 window.name。

    要获得比这更具体的信息,需要有关具体情况的更多信息。但是,在任何情况下,子脚本可以与父脚本通信或反之亦然,它们总是可以通过名称来识别彼此,这通常就足够了。

    【讨论】:

      【解决方案6】:

      我选择从@angular/platform-browser 注入DOCUMENT 令牌:

      import { DOCUMENT } from '@angular/platform-browser'
      

      然后访问父级:

      constructor(@Inject(DOCUMENT) private document: any) {
      }
      
      public ngOnInit() {
        // this.document.defaultView || this.document.parentWindow;
      }
      

      【讨论】:

        猜你喜欢
        • 2016-06-03
        • 1970-01-01
        • 1970-01-01
        • 2010-11-17
        • 1970-01-01
        • 2022-12-15
        • 1970-01-01
        • 1970-01-01
        • 2013-08-11
        相关资源
        最近更新 更多