Andrew
Andrew

Reputation: 238747

jQuery: How to manipulate the contents of an iframe when the src is a different domain?

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?

Upvotes: 2

Views: 1421

Answers (2)

AlexanderJohannesen
AlexanderJohannesen

Reputation: 2028

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.

Upvotes: 1

Pekka
Pekka

Reputation: 449495

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

Upvotes: 6

Related Questions