267

How do you get a <div> from within an <iframe>?

joepour
  • 6,102
  • 10
  • 28
  • 29
  • [w3schools Get Element in Iframe](https://www.w3schools.com/howto/howto_js_element_iframe.asp) – Carson May 10 '21 at 07:53

8 Answers8

412
var iframe = document.getElementById('iframeId');
var innerDoc = (iframe.contentDocument) ? iframe.contentDocument : iframe.contentWindow.document;

You could more simply write:

var iframe = document.getElementById('iframeId');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;

and the first valid inner doc will be returned.

Once you get the inner doc, you can just access its internals the same way as you would access any element on your current page. (innerDoc.getElementById...etc.)

IMPORTANT: Make sure that the iframe is on the same domain, otherwise you can't get access to its internals. That would be cross-site scripting.

Tamer Shlash
  • 8,675
  • 4
  • 41
  • 74
geowa4
  • 36,556
  • 14
  • 85
  • 105
  • 5
    Great answer. Did you know that you can do: var innerDoc = iframe.contentDocument || iframe.contentWindow.document; //more readable and means the same – David Snabel-Caunt Jul 06 '09 at 18:40
  • 5
    I've seen people confused by that more than ternary operators. They seem to not know that the first valid one is returned. – geowa4 Jul 06 '09 at 18:44
  • 6
    in fact, as i edited the answer to include it, the guy over my shoulder asked me what that did... – geowa4 Jul 06 '09 at 18:46
  • 13
    Probably better to educate than use more complex code for simplicity :) – David Snabel-Caunt Jul 07 '09 at 11:28
  • I know and found the above answer is correct, but while doing innerDoc.getElementById("idab") I got an error like "Error: Permission denied to access property 'getElementById'". Do you know why this error came. – Deepak Lamichhane Dec 19 '11 at 01:15
  • I also get an error (in Chrome) when trying to access an Element in an iframe whose domain differs from the top page. Any way around this? – JellicleCat Aug 08 '12 at 23:06
  • 1
    @JellicleCat: Any way around that is a way to do "Cross-site request forgery", also known as "one-click" attack or "session riding" – CSharper Jan 10 '13 at 18:57
  • sometimes trying to get the content of the iframe returns null, be sure to check for that. – Udo Feb 24 '16 at 03:24
  • While this may error out on a hosted page, running a local file in the browser with this code worked, at least on Safari and a publicly shared printer's web server. – cde Aug 10 '16 at 06:18
  • What's the difference between `iframe.contentDocument` and `iframe.contentWindow.document`? – Stevoisiak Jul 12 '18 at 19:14
  • @StevenM.Vascellaro Cross browser – Rahil Wazir May 24 '19 at 11:15
  • Is it possible to do cross-site scripting?? i wanna do a scrapper... – MonneratRJ Dec 02 '20 at 05:03
14

Do not forget to access iframe after it is loaded. Old but reliable way without jQuery:

<iframe src="samedomain.com/page.htm" id="iframe" onload="access()"></iframe>

<script>
function access() {
   var iframe = document.getElementById("iframe");
   var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
   console.log(innerDoc.body);
}
</script>
lukyer
  • 6,141
  • 1
  • 31
  • 28
5

Above answers gave good solutions using Javscript. Here is a simple jQuery solution:

$('#iframeId').contents().find('div')

The trick here is jQuery's .contents() method, unlike .children() which can only get HTML elements, .contents() can get both text nodes and HTML elements. That's why one can get document contents of an iframe by using it.

Further reading about jQuery .contents(): .contents()

Note that the iframe and page have to be on the same domain.

Community
  • 1
  • 1
SkuraZZ
  • 753
  • 1
  • 9
  • 12
4
window.parent.document.getElementById("framekit").contentWindow.CallYourFunction('pass your value')

CallYourFunction() is function inside page and that function action on it

Roko C. Buljan
  • 164,703
  • 32
  • 260
  • 278
VishalDream
  • 281
  • 2
  • 6
2

None of the other answers were working for me. I ended up creating a function within my iframe that returns the object I was looking for:

function getElementWithinIframe() {
    return document.getElementById('copy-sheet-form');
}

Then you call that function like so to retrieve the element:

var el = document.getElementById("iframeId").contentWindow.functionNameToCall();
craned
  • 2,795
  • 2
  • 31
  • 38
1

If iframe is not in the same domain such that you cannot get access to its internals from the parent but you can modify the source code of the iframe then you can modify the page displayed by the iframe to send messages to the parent window, which allows you to share information between the pages. Some sources:

Jose V
  • 1,058
  • 1
  • 10
  • 26
1

You can use this function to query for any element on the page, regardless of if it is nested inside of an iframe (or many iframes):

function querySelectorAllInIframes(selector) {
  let elements = [];

  const recurse = (contentWindow = window) => {

    const iframes = contentWindow.document.body.querySelectorAll('iframe');
    iframes.forEach(iframe => recurse(iframe.contentWindow));
    
    elements = elements.concat(contentWindow.document.body.querySelectorAll(selector));
  }

  recurse();

  return elements;
};

querySelectorAllInIframes('#elementToBeFound');

Note: Keep in mind that each of the iframes on the page will need to be of the same-origin, or this function will throw an error.

Cary Meskell
  • 117
  • 1
  • 6
-9

Below code will help you to find out iframe data.

let iframe = document.getElementById('frameId');
let innerDoc = iframe.contentDocument || iframe.contentWindow.document;
  • 3
    Why repeat part of the suggestions from the accepted answer posted 9 years earlier? At least deleting this answer would earn you a _Peer Pressure_ badge… – Sebastian Simon Aug 24 '20 at 02:35