iframe在非IE瀏覽器指定Src及動態改變高度的問題

在IE瀏覽器內想要改變iframe頁框的Src值,只需要直接指定iframe.src='XXXX.htm';,如此就可以改變連結的路徑,但是其他瀏覽器可就不吃這一套了(至少我試了Firefox,Chrome都是如此)。

1. 在其它瀏覽器內iframe若要指定src有反應,必須要補齊所需要的網頁元素。如html,head,body......

2. 另外若要動態指定iframe的高度IE瀏覽器也與其它瀏覽器有所不同IE  → iframe.Document.body.scrollHeightOther → iframe.contentDocument.body.offsetHeight

詳細說明與法如下
var iframe = document.getElementById(obj);
if (iframe.src == "") {
var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
iframeDocument.open();
iframeDocument.writeln('');
iframeDocument.close();
iframe.src = 'XXXX.html';
}
if (document.getElementById) {
if (iframe && !window.opera) {
if (iframe.contentDocument && iframe.contentDocument.body.offsetHeight) {
iframe.height = iframe.contentDocument.body.offsetHeight;
} else if (iframe.Document && iframe.Document.body.scrollHeight) { //IE
iframe.height = iframe.Document.body.scrollHeight + 30;
}
}
}

留言