简体   繁体   English

Javascript,从opener调用子窗口函数不起作用

[英]Javascript,calling child window function from opener doesn't work

I'm developing a web application that opens a popup using windows.open(..). 我正在开发一个使用windows.open(..)打开弹出窗口的Web应用程序。 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. 我需要使用“window.open”返回的句柄在打开的窗口上调用一个函数,但我总是收到错误消息“addWindow.getMaskElements不是函数”,好像它无法访问声明的函数在儿童窗口。 This is the behavior in both IE and FF. 这是IE和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 ();
    }
}

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. 还有一件事,子窗口中的函数在adicionar_email.htm文件中包含的单独的.js文件中声明。 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. 提前致谢。

Kenia Kenia

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. 当该窗口打开并加载HTML和javascript并解析它时,脚本继续执行。

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.) (我试过这只是为了确定。)

**EDIT ** **编辑**

Someone else posted a workaround by calling an onload in the target document, here's another approach: 其他人通过在目标文档中调用onload来发布变通方法,这是另一种方法:

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(); }
}

This keeps trying to call addWindow.myRemoteFunction every 1 second til it manages to sucessfully call it. 这一直试图每隔1秒调用addWindow.myRemoteFunction直到它成功调用它。

The problem is that window.open returns fairly quickly, the document that is requested and then any other items that that document may subsequently refer to will not yet have been loaded into the window. 问题是window.open返回得相当快,所请求的文档以及该文档随后可能引用的任何其他项目尚未加载到窗口中。

Hence attempting to call this method so early will fail. 因此,尝试如此早地调用此方法将失败。 You should attach a function to the opened window's load event and attempt to make you calls from that function. 您应该将函数附加到打开的窗口的加载事件,并尝试从该函数调用。

The problem with the below one is : 以下问题是:

When the javascript is being executed in the parent window, the child window is not loading. 当在父窗口中执行javascript时,子窗口未加载。 Hence, the invoking function from parent window is in the infinite loop and it is leading to crashing the window. 因此,父窗口的调用函数处于无限循环中,导致窗口崩溃。

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. 当该窗口打开并加载HTML和javascript并解析它时,脚本继续执行。

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.) (我试过这只是为了确定。)

**EDIT ** **编辑**

Someone else posted a workaround by calling an onload in the target document, here's another approach: 其他人通过在目标文档中调用onload来发布变通方法,这是另一种方法:

 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(); } } 

This keeps trying to call addWindow.myRemoteFunction every 1 second til it manages to sucessfully call it. 这一直试图每隔1秒调用addWindow.myRemoteFunction直到它成功调用它。

You are calling the function immediately after opening the window; 打开窗口后立即调用该函数; the page on the popup may not be loaded yet, so the function may not be defined at that point. 弹出窗口上的页面可能尚未加载,因此可能无法在该点定义该功能。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM