[英]Dynamic IFrame resize not working in IE?
我有一些動態調整iframe的代碼:
function autoResizeFrames() {
frames = document.getElementsByTagName("iFrame");
window.setInterval("autoresize_frames()", 400);
}
function autoresize_frames() {
for (var i = 0; i < frames.length; ++i) {
if (frames[i].contentWindow.document.body) {
var frames_size = frames[i].contentWindow.document.body.offsetHeight;
if (document.all && !window.opera) {
frames_size = frames[i].contentWindow.document.body.scrollHeight;
}
frames[i].style.height = frames_size + 'px';
}
}
}
此功能在Firefox和Chrome中運行良好,但在IE 10和9中它具有mo效果。
這是我有一個IFrame的地方
<div id="projectModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="projectModalLabel"
aria-hidden="true">
<div class="modal-header">
<button type="button" runat="server" onserverclick="btnClose_Click" class="close" aria-hidden="true">
×</button>
<h3 id="projectModalLabel">Edit Project</h3>
</div>
<div class="modal-body">
<iframe src="" style="width: 100%; height: 200px; border: none;" frameborder="0" id="projectFrame" name="projectFrame" scrolling="no" allowtransparency="true"></iframe>
</div>
<div class="modal-footer">
<button type="button" runat="server" onserverclick="btnClose_Click" class="btn" aria-hidden="true">
Close</button>
</div>
</div>
為什么除了IE以外它在哪里都有用? 我認為javascript無處不在?
謝謝
它說:
SCRIPT5007: Unable to get property 'document' of undefined or null reference
kezcommon.js, line 62 character 5
if (frames.contentWindow.document.body) {
window.frames
是一個原生DOM集合,您正在分配一個具有相同名稱的節點列表。 不知何故,IE弄亂了這兩個frames
,這兩個frames
並不相似。
autoresize_frames()
似乎使用window.frames
集合而不是frames
nodelist(在autoResizeFrames()
定義)。 但是,當通過此集合訪問iframe
時,IE中沒有contentWindow
或contentDocument
,因為window.frames
包含iframe
的實際window
對象。
您需要直接訪問frames[i].document
或frames[i]
,這是iframe
的window
。
quickfix將使用一些其他名稱而不是節點列表的frames
。
嘗試
window.setInterval(autoresize_frames, 400);
只是為了看看設置回調的替代方法是否有效。 您可能還需要將autoresize_frames()函數放在autoResizeFrames()函數上方,以確保它在setInterval()調用之前存在於內存中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.