1

我有一个必须跨越多个框架的弹出窗口,所以我使用 window.createPopup 来让它工作。(IE6、7 和 8。)

下面是我用来创建弹出窗口的函数:

function ShowMyPopup() {
    notificationPopup = window.createPopup();
    $(notificationPopup.document.body).load("/notification.html");
    notificationPopup.show($(sourceFrame.document.body).width() - 510, $(sourceFrame.document.body).height() - (510 - $(sourceFrame.document.body).height()), 500, 500, sourceFrame.document.body);
}

这似乎工作得很好。我应该看到弹出窗口。问题是,无论我做什么,我似乎都无法访问生成的弹出窗口中的任何 DOM 元素。我尝试了各种 jQuery 方法以及直接的 getElementById,并且都返回 NULL。以下是notification.html的内容:

<html>
<head>
    <script type="text/javascript">
        $(document).ready(function () {
            alert($(document).html());
            alert($("#divNotification").html());
            alert(document.getElementById("divNotification"));
        });
    </script>
</head>
<body>
    <div id="divNotification" onclick="$(this).toggle();">
        <h3>Some Notification!</h3>
        Testing 1234...
    </div>
</body>
</html>

我看到三个警报,所以我知道 jQuery 正在工作,但所有三个警报都只显示“NULL”。如果我单击生成的 div,onClick 会触发,但会出现“预期对象”错误。

这里发生了什么?

4

2 回答 2

0

According to this reference, with window.createPopup() "scripts must assign content (not an external URL) to the popup object returned by the method".

If you want to assign content with an external URL then you need to use the non-proprietary (cross-browser) method window.open().

With window.open(), communication between the main window and the popup window can still be performed.

Assuming the popup to be opened with var myPopup = window.open(...);, then:

  • Main window addresses popup as myPopup
  • Popup addresses main window as opener
于 2012-04-16T20:12:20.860 回答
0

Ok. I figured this out. It's pretty counter intuitive.

Basically, I ended up loading all relevant javascript libraries into the source frame. I then must explicitly reference the source frame by its name in order to access these methods.

If any javascript - even javascript inside the popup window - wants to access the popup window's DOM, you must fully qualify it (or provide jQuery will the correct root object) for it to work.

For instance, here is my new notification.html:

<div id="divNotification" onclick="top.SourceFrame.MakePopupRed();">
    <h3>Some Notification!</h3>
    Testing 1234...
</div>

Now, in my source frame's referenced javascript library:

function MakePopupRed() {
    if (notificationPopup) {
        $("#divNotification", notificationPopup.document).css("background-color", "red");
    }
}

So, basically, it appears that javascript that is run inside a popup created from window.createPopup executes in the context of the parent window, rather than the popup window itself!

于 2012-04-17T18:37:16.257 回答