0

I have a snippet on www.a.com

<iframe src="www.b.com" id="my-iframe">
  #document
    <html>
      <header>
        //some headers go here
      </header>
      <body>
        <div id="my-iframe-div"></div>
      </body>
    </html>
 </iframe>

What I want to achieve is to add one class name on the div inside the iframe

so what I did is

const element = document.getElementById('my-iframe').contentWindow.document.getElementById('my-iframe-div');
if(element) element.classList.add('my-iframe-class')

which would result in cross domain error, because the src attribute is www.b.com, but the page is on www.b.com

How to implement that? thanks

Benjamin Li
  • 1,687
  • 7
  • 19
  • 30
  • 1
    cross domain iframe access can be achieved through [postMessage](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage) - because using postMessage allows code on both domains to allow and control such access – Jaromanda X Aug 21 '19 at 01:12
  • Possible duplicate of [Ways to circumvent the same-origin policy](https://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy) – Nickolay Aug 24 '19 at 10:46

0 Answers0