Return ID of all iframes in a page

前端 未结 3 727
我寻月下人不归
我寻月下人不归 2021-01-18 22:38

Because of the widget format I\'m working with I have a page which has multiple iframes embedded within iframes. I won\'t paste the code as it\'s vast and unwieldy but it is

3条回答
  •  粉色の甜心
    2021-01-18 23:05

    EDITED

    This returns an iframe element which has the wanted id, and null, if the wanted id is not found.

    function searchIds(wantedId) {
        var idArray = [], n,
            search = function (iframes) {
                var n;
                for (n = 0; n < iframes.length; n++) {
                    if (iframes[n].frames.length > 0) {
                        search(iframes[n].frames);
                    }
                    idArray.push(iframes[n].frameElement.id);
                    idArray.push(iframes[n].frameElement);
                }
            };
        search(window.top.frames);
        for (n = 0; n < idArray.length; n += 2) {
            if (idArray[n] === wantedId) {
                return idArray[n + 1];
            }
        }
        return null;
    }
    

    Notice, that searchIds() can't be run before onload of the main window has been fired.

提交回复
热议问题