getElementsByTagName is null or undefined, only in IE - and only in one particular spot in the function [duplicate]

Deadly 提交于 2019-12-23 17:03:13

问题


Possible Duplicate:
getElementById.contentDocument error in IE

listed below is my javascript code. What it does is searches the whole document for all iframes, then searches the iframes for all images. The parent and child frames are all my code on the same domain. The code works fine in chrome, FF and safari, but not IE. Here is the error I'm getting.

Microsoft JScript runtime error: Unable to get value of the property 'getElementsByTagName': object is null or undefined

And here is my code.

<script language="javascript">
var IMGmatches = [];
var IMGelems = document.getElementsByTagName("img");
var iframes = document.getElementsByTagName('iframe');
var l = IMGelems.length;
var m = iframes.length;
var i;
var j;
for (i = 0; i < l; i++) IMGmatches[i] = IMGelems[i];
for (j = 0; j < m; j++) {
    IMGelems = iframes[j].contentDocument.getElementsByTagName("img");
    l = IMGelems.length;
    for (i = 0; i < l; i++) {
        IMGmatches.push(IMGelems[i]);
        document.getElementById("HM").src = IMGelems[i].src;
        alert('IMGelems[i].src : ' + IMGelems[i].src);
    }
}

The error is happening at line:

IMGelems = iframes[j].contentDocument.getElementsByTagName("img");

Does anyone see or know why this could be happening only in IE? Thank you for your help.


回答1:


From MDN documentation:

The active document in the inline frame's nested browsing context. Not supported in Internet Explorer 7 and earlier; use contentWindow.document instead.



来源:https://stackoverflow.com/questions/11156622/getelementsbytagname-is-null-or-undefined-only-in-ie-and-only-in-one-particul

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!