如何在父窗口中更改url后在父窗口中运行子窗口java脚本函数?

时间:2012-10-15 11:17:10

标签: java php javascript

我想在父窗口上执行子窗口的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后执行子窗口函数。

1 个答案:

答案 0 :(得分:0)

我看到的唯一方式:

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

当然,这只适用于两个文件都放在同一个域下的情况。