Javascript,从开启器调用子窗口函数不起作用

2022-01-20 00:00:00 function popup javascript window.open

我正在开发一个使用 windows.open(..) 打开弹出窗口的 Web 应用程序.我需要使用window.open"返回的句柄在打开的窗口上调用一个函数,但我总是收到错误消息addWindow.getMaskElements is not a function",好像它无法访问声明的函数在子窗口上.这是 IE 和 FF 中的行为.我的代码如下所示:

I'm developing a web application that opens a popup using windows.open(..). I need to call a function on the opened window using the handle returned by "window.open", but I'm always getting the error message "addWindow.getMaskElements is not a function", as if it couldn't access the function declared on child window. This is the behavior in both IE and FF. My code looks like this:

function AddEmail(target,category)
{
    if(addWindow == null)
    {
        currentCategory = category;
        var left = getDialogPos(400,220)[0];
        var top =  getDialogPos(400,220)[1];
        addWindow = window.open("adicionar_email.htm",null,"height=220px, width=400px, status=no, resizable=no");
        addWindow.moveTo(left,top);
        addWindow.getMaskElements ();
    }
}

我已经用谷歌搜索并阅读了不同的可靠来源,显然这应该有效,但它没有.另一件事是,子窗口中的函数在 adicionar_email.htm 文件中包含的单独 .js 文件中声明.这有什么不同吗?不应该..所以,如果有人遇到过类似的问题,或者知道我做错了什么,请回复此消息.提前致谢.

I've googled and read from different reliable sources and apparently this is supposed to work, however it doesn't. One more thing, the functions in child window are declared in a separate .js file that is included in the adicionar_email.htm file. Does this make a difference? It shouldn't.. So, if anyone has ran into a similar problem, or has any idea of what I'm doing wrong, please, reply to this message. Thanks in advance.

肯尼亚

推荐答案

窗口创建不是阻塞操作;脚本在该窗口打开并加载 HTML & 时继续执行javascript并解析它.

The window creation is not a blocking operation; the script continues to execute while that window is opening and loading the HTML & javascript and parsing it.

如果您要在原始页面上添加这样的链接:

If you were to add a link on your original page like this:

<a href="#" onclick="addWindow.getMaskElements();">Test</a>

你会看到它有效.(我试了一下只是为了确定.)

You'd see it works. (I tried it just to be sure.)

**编辑**

其他人通过在目标文档中调用 onload 发布了解决方法,这是另一种方法:

Someone else posted a workaround by calling an onload in the target document, here's another approach:

function AddEmail()
{

        if(addWindow == null) {
        addWindow = window.open("test2.html",null,"height=220px, width=400px, status=no, resizable=no");
        }

        if(!addWindow.myRemoteFunction) {
            setTimeout(AddEmail,1000);
        } else { addWindow.myRemoteFunction(); }
}

这会一直尝试每 1 秒调用一次 addWindow.myRemoteFunction,直到成功调用它.

This keeps trying to call addWindow.myRemoteFunction every 1 second til it manages to sucessfully call it.

相关文章