IFrame call parent function

2019-07-14 13:54发布

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

2条回答
手持菜刀,她持情操
2楼-- · 2019-07-14 14:22

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

查看更多
3楼-- · 2019-07-14 14:25

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.

查看更多
登录 后发表回答