Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从另一个HTML页面调用函数?_Javascript_Html_Function_Window.open - Fatal编程技术网

Javascript 如何从另一个HTML页面调用函数?

Javascript 如何从另一个HTML页面调用函数?,javascript,html,function,window.open,Javascript,Html,Function,Window.open,我想从我创建的另一个HTML网页调用一个函数 如果第二个Javascript函数为: function g(x){ alert(x); } 所以我在第一页尝试了这个: function f(){ var x = prompt("enter"); testwindow = window.open("otherWebPage.html", "_self"); testwindow.g(x); } f(); 但它不起作用。我哪里出错了 (这不是我在页面中的代码,我只是

我想从我创建的另一个HTML网页调用一个函数

如果第二个Javascript函数为:

function g(x){
   alert(x);
}
所以我在第一页尝试了这个:

function f(){
    var x = prompt("enter");
    testwindow = window.open("otherWebPage.html", "_self");
    testwindow.g(x);
}
f();
但它不起作用。我哪里出错了


(这不是我在页面中的代码,我只是给出了一个简单的示例)

问题是在
otherWebPage.html
中的代码运行之前,您正在调用
testwindow.g
。您需要等待必要的功能可用

有几种方法可以做到这一点,但下面是一个简单的示例,说明如何通过等待
load
事件来做到这一点

function f(){
    var x = prompt("enter");
    testwindow = window.open("otherWebPage.html", "_self");
    testwindow.addEventListener('load', function(){
        testwindow.g(x);
    });
}
f();

问题是您在运行
otherWebPage.html
中的代码之前调用了
testwindow.g
。您需要等待必要的功能可用

有几种方法可以做到这一点,但下面是一个简单的示例,说明如何通过等待
load
事件来做到这一点

function f(){
    var x = prompt("enter");
    testwindow = window.open("otherWebPage.html", "_self");
    testwindow.addEventListener('load', function(){
        testwindow.g(x);
    });
}
f();
问题是

window.open("otherWebPage.html", "_self");
在当前页面中加载“otherWebPage.html”,并将其卸载

而且一个未加载的页面不能调用函数。

问题是

window.open("otherWebPage.html", "_self");
在当前页面中加载“otherWebPage.html”,并将其卸载


卸载的页面无法调用函数。

JavaScript错误控制台怎么说?可能会重复将加载处理程序绑定到新窗口。必须加载新页面才能使函数在那里。您是否尝试过给它一个超时?JavaScript错误控制台怎么说?可能会重复将加载处理程序绑定到新窗口。必须加载新页面才能使函数在那里。您是否尝试过给它一个超时时间?@AmalPrasad肯定还有其他原因在起作用。您可以尝试制作一个MCVE并发布一个新问题以寻求调试帮助。@AmalPrasad肯定还有其他原因在起作用。您可以尝试制作一个MCVE并发布一个新问题以寻求调试帮助。