2012-12-12 105 views
0

我正在嘗試調整使用javascript的網站上iframe的大小,以便iframe與內容一樣長。我的代碼如下:使用javascript調整iframe的大小

var iFrames = $('iframe'); 

function iResize() {   
    for (var i = 0, j = iFrames.length; i < j; i++) { 
     iFrames[i].style.height = iFrames[i].contentWindow.document.body.offsetHeight + 'px'; 
    } 
} 

console.log('resize frame before'); 
if ($.browser.safari || $.browser.opera) { 
    console.log('if');    
    iFrames.load(function(){ 
     setTimeout(iResize, 0); 
    }); 

    for (var i = 0, j = iFrames.length; i < j; i++) { 
     var iSource = iFrames[i].src; 
     iFrames[i].src = ''; 
     iFrames[i].src = iSource; 
    }    
} else { 
    console.log('else'); 
    $('.iframe').load(function() { 
     console.log('load'); 
     this.style.height = this.contentWindow.document.body.offsetHeight + 'px'; 
    }); 
} 
console.log('resize frame end'); 

它不工作,但與console.log沒有出現的唯一日誌是'負載'函數內。

我打電話給頁面底部的所有javascript,我不確定是否導致問題。

任何幫助將大大appreaciated。


編輯:源代碼是在https://github.com/xonorageous/projet-sas 網站可以在http://daniel-lucas.com/experiments-dir/projet-sas/

+0

「iframe」的來源是否在同一個域上?不要忘記相同的域策略。 – BenM

+0

是的,我的iframe的代碼是