简体   繁体   English

更改父窗口中的URL后如何在父窗口中运行子窗口Java脚本函数?

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

I want to execute the java script function of a child window on parent window, Even after changing the URL in the parent window. 我想在父窗口上执行子窗口的Java脚本功能,即使更改了父窗口中的URL也是如此。 I tried like below: 我尝试如下:

**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>

Clearly, my concept is: I want to execute the child window function after changing the url in the parent window. 显然,我的概念是:我想在更改父窗口中的url之后执行子窗口功能。

The only way I see: 我看到的唯一方法:

observe the unload-event of the opener. 观察开瓶器的卸载事件。 When it fires, try(after a short timeout) to reassign the onclick-function(and also the onunlad-function). 当它触发时,尝试(在短暂的超时后)重新分配onclick函数(以及onunlad函数)。

Of course this will only work when both documents are placed under the same domain. 当然,这仅在两个文档都位于同一域下时才有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaFX中关闭子窗口后刷新父窗口? - How to refresh parent window after closing child window in JavaFX? 在子项提交后如何在父窗口中重新加载文本 - How to reload a text on parent window after submit on child's 单击Java中子窗口中的链接后,未在父窗口中设置json数据 - json data not set in parent window after clicking a link from child window in java 关闭子窗口时关闭父窗口 - Parent Window Closing when Closing Child Window 如何关闭父窗口并在子窗口上继续执行 - How to close parent window and continue execution on a child window 单击某些父窗口的子窗口后出现的弹出窗口的处理方法 - How to handle pop up which came after i clicked on child window of some parent window 如何在提交表单时关闭弹出窗口时重新加载父窗口,而我正在使用java类显示子窗口内容 - how to reload the parent window while closing popup window when on form submission i am using java class to show the child window content 刷新父窗口和子窗口后,保持子窗口的焦点 - Retain focus of Child window after parent and child windows are refreshed 如果子窗口在单击按钮时关闭,如何将控制从子窗口切换到父窗口? - How to switch control from child window to parent window if the child window close on clicking a button? 如何在Java + SWT中将对象停靠到父窗口? - How to Dock object to parent Window in Java + swt?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM