2

I have an iframe loading a page, and I would like to manipulate the contents.

For example:

<iframe id="myIframe" src="http://www.google.com"></iframe>

<script>
    $(document).ready(function(){
        $(iframeContents).css('background-color', '#000');
    });
</script>

How can I do this?

Andrew
  • 227,796
  • 193
  • 515
  • 708
  • 1
    possible duplicate of [jquery/javascript: accessing contents of an iframe](http://stackoverflow.com/questions/364952/jquery-javascript-accessing-contents-of-an-iframe) – Pekka Oct 27 '10 at 21:50

2 Answers2

6

You can't. The Same Origin Policy forbids this.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
1

As Pekka said, you can't, not directly. The JavaScript security model prohibits this. However, you can do it if the domain in the iFrame is the same as the one in which the script runs. So what can be done is to set up a proxy on your own domain, and show the content in the iFrame through the proxy and then use JS to interact with it.

AlexanderJohannesen
  • 2,028
  • 2
  • 13
  • 26