0

我想在父窗口上执行子窗口的 java 脚本函数,即使更改了父窗口中的 URL。我尝试如下:

**Parent.html:**

<html>
  <head>
    <title>Parent window document</title>
  </head>
  <body>
  <script type="text/Javascript">
    function openChildWindow()
    {
      var s_url     = "child.html";
      var s_name    = "ChildWindowDocument";
      var s_specs   = "resizable=yes,scrollbars=yes,toolbar=0,status=0";
      var childWnd  = window.open(s_url, s_name, s_specs);
      var div       = childWnd.document.getElementById("child_wnd_doc_div_id");
      div.innerHTML = "Hello from parent wnd dddsfds";
    }
  </script>
  <input type="button" value="Open child window document" name="sss" onclick="openChildWindow()" />
    <div>Click me: nothing will happen.</div>
    <div id="aaa" class="yourclass">
        <p>This is a paragraph with a <span>span</span> where the paragraph's container has the class. Click the span or the paragraph and see what happens.</p>
        This sentence is directly in the div with the class rather than in child element.
    </div>
    <div>Nothing for this div.</div>
    <a>dsfsd</a>
  </body>
</html>

**child.html:**

<html>
  <head>
    <title>Parent window document</title>
    <script>
    opener.document.body.onclick = function(e) {
    var sender = (e && e.target) || (window.parent.event && window.parent.event.srcElement);
    document.getElementById("id").value=sender.tagName;
    }
    </script>
  </head>
  <body>
    <div id="child_wnd_doc_div_id">child window</div>
    ID: <input type="text" id="id"/><br/>
    Name: <input type="text" id="id"/><br/>
    Xpath: <input type="text" id="id"/><br/>
    CSS: <input type="text" id="id"/><br/>
  </body>
</html>

显然,我的概念是:我想在改变父窗口中的url后执行子窗口功能。

4

1 回答 1

0

我看到的唯一方式:

观察开瓶器的卸载事件。当它触发时,尝试(在短暂超时后)重新分配 onclick 功能(以及 onunlad 功能)。

当然,这只有在两个文档都放在同一个域下时才有效。

于 2012-10-15T22:53:55.130 回答