2011-10-14 76 views
0

我需要调整跨域iframe的大小以适应内容。因此,在parent.html我写这篇文章:如何调整IFRAME大小?

$(function() { 
    $('iframe').load(function() { 
     var h = null; 
     if (!h) { 
      if (location.hash.match(/^#h(\d+)/)) { 
       h = RegExp.$1; 
       $(this).css({ 
        'height': h + 'px' 
       }) 
       location.hash = '' 
      } 

     } 
    }) 
}) 

和child.html我写这篇文章:

$(function() { 
    h = 0 
    h = $(document).height() 
    console.log(h) 
    if (top != self) { 
     top.location.replace("http://parentdomain.com/#h" + h); 
    } 
}) 

它工作正常时,它加载一次,但如果我点击在iframe和内容的变化链接变量h仍然具有第一价值。如果我在新窗口中打开child.htmlvar h将会更改,但不会在iframe中。

可以修复吗?

回答

0

resize操作实际上不会再被调用。

如果您在iFrame中单击某个链接,您应该在父级上调用一个函数,或者观察仅在最新浏览器上支持的haschange事件。

+0

但我得到'h'的孩子的dom准备好了。不是吗? – rsboarder

+0

只有第一次加载完成后 –

+0

我对jQuery了解不多,但我们已经多次使用绑定到iframe的onload事件完成:)所以每当内容发生更改时它都适合它的内容(使用ajax调用异常;) – walialu