1

I'm using an iframe and dynmically loading its content from an API. I'm trying to find a way to clear the iframe of all of it's content without changeing the src attribute. E.G.

<iframe class="bla" src="//blabla">
  <html>
     Lots of HTML rendered
  </html>
</iframe>

Needs to become:

<iframe class="bla" src="//blabla"></iframe>

Very important that I keep all of the iframe attributes. Is there a way to do this with javascript or jquery?

Shane_IL
  • 325
  • 1
  • 4
  • 16
  • 1
    If it's not from the same origin, you generally can't edit the markup inside it. – adeneo Mar 22 '15 at 15:00
  • Why is keeping the `src` attribute as is important in this scenario? If that wasn’t a requirement, you could simply set it to `about:blank`. – CBroe Mar 22 '15 at 15:19
  • I'm trying to not have to rerun all the logic I used for getting the attributes, just to empty the iframe and resend it to the API to be rendered again. – Shane_IL Mar 23 '15 at 10:58

1 Answers1

1

See Oscar Jara's answer to a similar question here:

https://stackoverflow.com/a/11658546/4669143

If the iframe origin in the same as the enclosing site's origin, you should be able to do it without violating the cross-domain policy. Otherwise, it won't work.

He includes a working fiddle.

Community
  • 1
  • 1
Benjamin Ray
  • 1,855
  • 1
  • 14
  • 32