Diogo Mendonça
Diogo Mendonça

Reputation: 923

IFrame call parent function

is it possible for an iframe to call a parent's function, even if they are not in the same domain? My actual approach lifts a security error when they are not on the same domain:

<script>
function test()
{ alert('wow');
}
</script>
<iframe src="...."></iframe>

And inside the iframe i would do this:

<script> 
function fin() 
{ top.test(); } 
</script>

Many thanks :)

Upvotes: 2

Views: 2989

Answers (2)

Quentin
Quentin

Reputation: 944546

You can't call the function directly (due to the same origin policy, but you can use postMessage (as described in an answer to this question) and have an event listener call that function in response.

You're going to be somewhat out of luck if you need to support Internet Explorer 7 and lower.

Upvotes: 4

ckruse
ckruse

Reputation: 9740

No, you can't. The same origin policy forbids this.

Upvotes: 2

Related Questions