简体   繁体   中英

js cross-domain to iframe

I have the iframe from different domain imbedded in my page. Is it possible to have access to its DOM from a parent page with some way? Now it says:


Uncaught DOMException: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin " http://localhost:63342 " from accessing a cross-origin frame.


I hoped that adding the directive

<?php header('Access-Control-Allow-Origin: *');  ?>

to the iframe page will help, but it doesn't. Both domains now are located on my localhost but under different servers running. So is there some working solution? Notice that I need just an access to some iframe's elements, it's not about postMessages, websockets etc.

Notice that I need just an access to some iframe's elements

Direct access is impossible

it's not about postMessages

postMessage can achieve the same ultimate goal.

You just need to move the code which accesses the DOM to the page inside the iframe, and then use postMessage from the parent window to ask that page to run it. That page can then serialise any data the parent window needs to a string and use postMessage to send it back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM