IFrame call parent function

2019-07-14 14:09发布

问题:

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 :)

回答1:

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.



回答2:

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