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

前端 未结 4 1273
南旧
南旧 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:

    Test
    

    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.

提交回复
热议问题