How can I send an event from child window to its parent window

前端 未结 3 685
有刺的猬
有刺的猬 2021-02-05 16:27

My main goal is:

Going to my application, open a link there in a new tab, make something in the new tab and send an event to the parent-main tab to refresh.

3条回答
  •  花落未央
    2021-02-05 16:35

    The following works for me in chrome, firefox, ie(didn't test more browsers)

    assume 3 documents

    1. (www.mydomain.com/parent.html)the page that contains the 'main'-document with the link
    2. (bills.mydomain.com/child.html)the page that will be opened by the link
    3. (www.mydomain.com/dispatcher.html)explained later

    at first set the domain-property of all 3 documents to mydomain.com

    
    

    in parent.html create a hidden iframe with a name-property of e.g. "hiddenframe". Also create some function that may later receive a response.

    parent.html should now look like this:

    
    
    click
    

    In child.html you'll now be able to load a document into the hidden iframe inside parent.html

    
    

    (don't be confused in face of the use of window.open() here, there will not open a new window, the page will be loaded into the iframe in parent.html)


    In dispatcher.html you now may call the function inside parent.html

    
    

    When you only need to reload the parent.html it's a little bit easier.

    Again set the document.domain-property in parent.html and child.html(you don't need the iframe in parent.html and the dispatcher.html)

    In parent.html also set the name-property of the window, e.g.

    
    

    In child.html you now may access the parentTab-window(tab)

    
    

    ...or simply use "parentTarget" as target-property of a link or form in child.html

提交回复
热议问题