运行 window.open javascript 原始页面中的函数

Run Function in original page from window.open javascript

如何运行在window.open

的原始页面中使用

original_page.html

<div id="Processing" style="display:none;">
<button onclick="window.open("Processing_window.html")">open</button>
</div>

<script language="JavaScript">
    function submit_form() {    
          var upgradeForm = document.getElementById('upgradeForm');
          setTimeout("upgradeForm.submit()",3000); 
     } 

</script>

========

Processing_window.html

<script language="JavaScript">
    function Processing_window() {  
        var doc = window.opener.document, Processing = doc.getElementById("Processing");
        Processing.style = '';
        submit_form();  //Here the problem
        window.close();
    } 
    setTimeout ("Processing_window()",5000); 
</script>

========

我从 Processing_window.html

去了 运行 "submit_form();" 函数

可以,但不能跨域。仅在同一域中。

<script>
function new_win_fun() {
var wind = window.open("Processing_window.html");
var upgradeForm = wind.document.getElementById("upgradeForm");
wind.setTimeout("upgradeForm.submit()",3000); 
}
</script>

<button onclick="new_win_func()">Run</button>

您需要HTML对属性中代码中的引号进行编码,或者使用撇号:

<button onclick="window.open('Processing_window.html')">open</button>

只要打开的页面同源(同服务器,同协议),就可以使用opener属性访问父windowswindow 对象。您可以在其中找到对函数的引用:

window.opener.submit_form();

要从 opener 访问函数,请使用 window.opener.functionName,假设两者在同一个域中。

function Processing_window() {  
    var doc = window.opener.document, Processing = doc.getElementById("Processing");
    Processing.style = '';
    // Here's the solution
    window.opener.submit_form();  
    window.close();
} 
setTimeout ("Processing_window()",5000);