使用postMessage的跨域iframe调整大小 [英] Cross domain iframe resizer using postMessage

查看:85
本文介绍了使用postMessage的跨域iframe调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了所有跨域iframe帖子(在此感谢所有人!)和其他地方.

I've read all the cross domain iframe posts here (my thanks to all of you!) and elsewhere.

跨域iframe调整程序中的postMessage脚本可以在以下环境中很好地工作:Firefox 5及更高版本.每次在iframe中完美点击页面时,它都会调整iframe的大小.

The postMessage script at cross-domain iframe resizer? works beautifully in Firefox 5 and up. It resizes the iframe every time a page is clicked within the iframe perfectly.

但是在计算机上的IE(7 8或9)中根本无法调整大小.我检查了安全设置,并选中了IE中的跨域访问设置,以启用该功能.

But it doesn't resize at all in IE (7 8 or 9) on my computer. I checked the security settings and the one in IE for access across domains was checked to enable.

postMessage在IE中不起作用吗?-还是需要添加其他内容?谢谢

Does postMessage not work in IE? - Or is there something else that needs to be added? thanks

推荐答案

这是thomax编写的出色脚本-它也可以使用,因此您可以在移动设备上使用iframe-iPhone和android

It's a great script from thomax - it also works on so you can use iframes on mobile - iphones and android

对于IE7和IE8,您必须使用window.attachEvent而不是window.addEventListenerps也应该是onmessage而不是消息(请参阅下文)ps,您还需要在服务器上执行同样的操作,内容要发布其大小

For IE7 and IE8, you have to use window.attachEvent instead of window.addEventListener It should also be onmessage instead of message (see below) ps you also need to do the same on the server with the content posting its size

<script type="text/javascript">
if (window.addEventListener)
{
  function resizeCrossDomainIframe(id) {
    var iframe = document.getElementById(id);
    window.addEventListener('message', function(event) {
      var height = parseInt(event.data) + 32; 
      iframe.height = height + "px";
    }, false);
  }
}
else if (window.attachEvent)
{
  function resizeCrossDomainIframe(id) {
    var iframe = document.getElementById(id);
    window.attachEvent('onmessage', function(event) {
      var height = parseInt(event.data) + 32; 
      iframe.height = height + "px";
    }, false);
  }
}
</script>

这篇关于使用postMessage的跨域iframe调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆