srgg6701
srgg6701

Reputation: 2038

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.

Upvotes: 0

Views: 887

Answers (1)

Quentin
Quentin

Reputation: 943089

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.

Upvotes: 1

Related Questions