Calling a parent window function from an iframe

后端 未结 10 2535
悲&欢浪女
悲&欢浪女 2020-11-22 01:43

I want to call a parent window JavaScript function from an iframe.



&         


        
10条回答
  •  天涯浪人
    2020-11-22 02:34

    I have posted this as a separate answer as it is unrelated to my existing answer.

    This issue recently cropped up again for accessing a parent from an iframe referencing a subdomain and the existing fixes did not work.

    This time the answer was to modify the document.domain of the parent page and the iframe to be the same. This will fool the same origin policy checks into thinking they co-exist on exactly the same domain (subdomains are considered a different host and fail the same origin policy check).

    Insert the following to the of the page in the iframe to match the parent domain (adjust for your doctype).

    
    

    Please note that this will throw an error on localhost development, so use a check like the following to avoid the error:

    if (!window.location.href.match(/localhost/gi)) {
        document.domain = "mydomain.com";
    } 
    

提交回复
热议问题