简体   繁体   中英

Cross browser solution for checking Null Iframes

I am struggling since quite a long time now, and finally I have to post a query since I havent received any relevant answer till now.

What I need is to check for Null Iframes and accordingly call a function. I am looking for a replacement for the below three lines of code so as to be able to work in all browsers rather than only in IE.

if (this.document.frames != null ) {
    for (var i=0;i<this.document.frames.length;i++){
        if (this.document.frames[i] != null ) {

PS: I cannot use getElementbyId or getElementsByTagName since the IFrames in my code are created dynamically. Sorry for my knowledge,but I am preety new to Javascript and seem to have tried too many things now :(

Thanks a lot in advance..

You can still use getElementbyId and getElementsByTagName for dynamically added elements as long as you look for the elements AFTER they have been added to the DOM.

To put it another way, you can only find them if they exist at that point in time you look for them.

var iframes = document.getElementsByTagName('iframe'); // notice this is not 'frame'

var length = iframes.length;
var currentIFrame;
var counter;

for (counter = 0; counter < length; counter += 1) {
    currentIFrame = iframes[counter];

    // do stuff with currentIFrame
}

http://jsfiddle.net/fuEsw/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM