Document.getElementsByTagName ('iframe') [0] cannot get the object, but can if you do not specify a subscript 0

questions are as follows

document.getElementsByTagName("iframe")[0]
document.getElementsByTagName("iframe")

Code and screenshot

var ifr = document.getElementsByTagName("iframe");
console.log(ifr);
console.log(ifr[0]);

clipboard.png

Apr.05,2021

are you sure which undefined is the result of the execution of ifr [0]? Not your console.log (xxxx)?


document.getElementsByTagName ('iframe'); returns a dynamic collection whose contents change each time the value is taken.

if between console.log (ifr); and console.log (ifr [0]), the iframe in the page is removed from the document, then console.log (ifr [0]) becomes undefined.. You can output ifr again after console.log (ifr [0]) to determine whether this situation has been sent.

console.log(ifr); 
console.log(ifr[0]);
console.log(ifr);  //  ifr 

has not been reproduced. I have tried it on this page and output it successfully. Please check the code
clipboard.png


lz

clipboard.png

.
frames || window.frames iframe
frames[index] || window.frames[index] || window.frames[iframe_id] iframe
frames[index].frameElement.contentWindow iframewindow
frames[index].frameElement.contentWindow.frames[sub_index].frameElemnet.contentWindow...
I hope it will be useful to you

Today, I encountered the same problem, and it is an occasional problem. Ios appears frequently. There was no such problem before. Since yesterday, the frequency of problems in testing has become higher. At the same time, the browser test on mac will show that getElementsByTagName ('iframe') [0] cannot get the corresponding iframe tag, and I don't know whether the landlord has solved the problem

.
Menu