窗口对象没有创建它的页面附加到它的属性

时间:2017-09-21 07:31:50

标签: javascript internet-explorer internet-explorer-11

我使用window.open(url, target)在新标签/窗口中打开url。在此之前,我在新打开的窗口对象上设置了一些属性,目的是将数据从此页面(新窗口的创建者)传递到新创建的窗口。

在除Internet Explorer之外的所有浏览器中,代码都可正常运行。新创建的页面能够从表示其窗口的全局window对象中读回属性。

但是,仅在Internet Explorer 11中,新创建的窗口中的window对象没有上一页传入的属性。

以下是演示此问题的示例代码。

Page1.html

<script type="text/javascript">
    function openNewPage(value1, value2)
    {
        var newWindow = window.open('Page2.html', '_blank')

        newWindow.property1 = value1;
        newWindow.property2 = value2;
    }
</script>

Page2.html

<body onload = "bodyLoadHandler()">
    <script type="text/javascript">
        function bodyLoadHandler() {

            // Both of these returned undefined
            // whether referenced explicitly
            // as window.property1 or as 
            // this.property1. I prefer the explicit
            // window.property1 way because of the screwed up
            // this pointer and its ensuing issues in JavaScript.

            // This happens only
            // in IE. The code runs fine in other browsers.
            var property1 = window.property1;
            var property2 = window.property2;
        }
    </script>
</body>

1 个答案:

答案 0 :(得分:1)

使用window.openerSharedWorkerpostMessagestorage事件

Page1.html

<script>
  function openNewPage(value1, value2) {
    var newWindow = window.open('Page2.html', '_blank')

    this.property1 = value1;
    this.property2 = value2;
  }
  openNewPage(1, 2)
</script>

Page2.html

<body onload="bodyLoadHandler()">
  <script type="text/javascript">
    function bodyLoadHandler() {
      var property1 = window.opener.property1;
      var property2 = window.opener.property2;
      console.log(property1, property2);
    }
  </script>
</body>

plnkr http://plnkr.co/edit/Gy5FFhvobFoBAbc8UnBY?p=preview

另请参阅How can I load a shared web worker with a user-script?Can the mechanism that loads the worker in Shared Web Workers be modified?