ahe
ahe

Reputation: 781

Can't change the background of an iframe

I'm simply trying to change the background of an iframe that contains Google. But I can't. Any suggestions ?

<script>
function changeFrame() {
    var oIframe = document.getElementById("myframe");
    var oDoc = oIframe.contentWindow || oIframe.contentDocument;
    if (oDoc.document) {
        oDoc = oDoc.document;
    }
    oDoc.body.style.backgroundColor = "#00f";
    return true;
}
</script>

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

Upvotes: 1

Views: 183

Answers (3)

megakorre
megakorre

Reputation: 2223

this is a wherry common question :D

your not allowed to interact with content from other domains then your own in javascript

Upvotes: 0

Alistair Evans
Alistair Evans

Reputation: 36503

It is a rule of javascript in web browsers that a script sourced from one domain may not manipulate the DOM of another domain, even when that other domain is shown in an iframe. This is called Same Origin Policy.

So I'm afraid this is not possible.

Upvotes: 0

Andy E
Andy E

Reputation: 344685

You can't do this, it would be in violation of the Same Origin Policy which prevents you from accessing and modifying content on another domain.

In computing, the same origin policy is an important security concept for a number of browser-side programming languages, such as JavaScript. The policy permits scripts running on pages originating from the same site to access each other's methods and properties with no specific restrictions, but prevents access to most methods and properties across pages on different sites.

Upvotes: 1

Related Questions