2014-09-03 2 views
0

Я использую iframe на моей странице. В соответствии с содержанием, мне нужно установить высоту iframe. Для этого я использую следующий код.Настройка динамической высоты iframe работает только на хроме

$('iframe').on('load',function(){ 
    $('iframe').attr('id','sop_iframe'); 
    $('#sop_iframe').height($('#sop_iframe').contents().height()) 
}); 

Это прекрасно работает в хромонике .. но не в firefox и IE. Есть ли какое-либо перекрестное решение для браузера jquery?

+0

Это было в основном обсуждается здесь: http://stackoverflow.com/questions/9975810/make-iframe-automatically-adjust-height-according-to-the-contents-without-using –

ответ

0
function setFrameHeight() { 

var h; 
var defaulth = 150; 
var scrollh = window.top.document.documentElement.scrollTop; 

//reset the iframe to default height 
window.top.document.getElementById("iframe_form").height = defaulth; 

//get actual heights 
var y1 = document.documentElement.scrollHeight; 
if (y1 < document.body.scrollHeight) y1 = document.body.scrollHeight; 
var y2 = document.documentElement.offsetHeight; 
if (y2 < document.body.offsetHeight) y2 = document.body.offsetHeight; 
h = (y1 > y2) ? y1 : y2; 

//determine smaller heights 
h = (h > defaulth) ? h : defaulth; 

//set "iframe_form" to proper height 
window.top.document.getElementById("iframe_form").height = h; 


//scroll to top 
window.top.scrollTo(0,scrollh); 

} 
Смежные вопросы