从window.open javascript在原始页面中运行Function

时间:2015-02-20 19:41:49

标签: javascript jquery function var

如何从window.open

在原始页面中运行Function

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>

======

我去运行“submit_form();”来自Processing_window.html

的功能

3 个答案:

答案 0 :(得分:1)

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

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

只要您打开的页面具有相同的来源(相同的服务器,相同的协议),您就可以使用opener属性来访问父窗口window对象。在那里你可以找到对函数的引用:

window.opener.submit_form();

答案 1 :(得分:1)

要从开场白访问功能,请使用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); 

答案 2 :(得分:0)

你可以,但不能跨域。只在同一个域中。

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