Skip to content Skip to sidebar Skip to footer

Get Iframe Of Given ID

How do you select the iframe DOM Object on the page if you know the iframe's ID? Assume there might be multiple iframes on the page and you don't know the position of the one in q

Solution 1:

See this answer:

Access iframe elements in JavaScript

window.frames['myIFrame']

EDIT:

I made some tests on FF and Chrome and came up with the following conclusions.

window.frames["myFrame"] returns:

    `[object HTMLIFrameElement]` when on FF (v24)
    `[object Window]` when on Chrome (v 30.0.1599.101)

we can make a control like this for HTMLIFrameElement property contentWindow to get the iframe Window:

var myFrame = (window.frames["myFrame"].contentWindow ? window.frames["myFrame"].contentWindow : window.frames["myFrame"])

This way in the variable myFrame is stored the Window object.

If now we want to get the DOM element HTMLIFrameElement we can simply do:

var domElement = myFrame.frameElement

Hope this will be useful.


Solution 2:

var el = document.querySelector('#test');

var matches = el.querySelectorAll('iframe');

readmore:http://slides.html5rocks.com/#new-selectors


Post a Comment for "Get Iframe Of Given ID"