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

感情迁移 提交于 2019-11-27 14:24:30

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

**EDIT **

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

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

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.

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

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:

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!