簡體   English   中英

更改父窗口中的URL后如何在父窗口中運行子窗口Java腳本函數?

[英]How to run a child window java script function in parent window after changing the url in the parent window?

我想在父窗口上執行子窗口的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之后執行子窗口功能。

我看到的唯一方法:

觀察開瓶器的卸載事件。 當它觸發時,嘗試(在短暫的超時后)重新分配onclick函數(以及onunlad函數)。

當然,這僅在兩個文檔都位於同一域下時才有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM