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

前端 未结 4 1255
南旧
南旧 2020-12-03 11:30

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

相关标签:
4条回答
  • 2020-12-03 11:52

    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.

    0 讨论(0)
  • 2020-12-03 11:56

    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.

    0 讨论(0)
  • 2020-12-03 11:58

    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.

    0 讨论(0)
  • 2020-12-03 12:06

    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.

    0 讨论(0)
提交回复
热议问题