91

I have a iframe like this

<iframe name="myframe1" id="myframe1" width="100%" height="100%" src="a.html">
<html>
    <head></head>
    <frameset name="myframe2" cols="0%, 100%" border="0" frameBorder="0" frameSpacing="0">
        <frame name="page1" src="c.html" scrolling="no"></frame>
        <frame name="page2" src="d.html" >
            <html>
                <head></head>
                <body id="top">
                    <div id="div1">
                        <div id="div2">
                            <div id="div3">
                                <ul id="x">
                                    <li>a</li>
                                    <li>b</li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </body>
            </html>

        </frame>

    </frameset>
</html>
</iframe>

I want to refer to the element "x". I tried in several ways but I couldn't find a solution.

Leigh
  • 28,765
  • 10
  • 55
  • 103
ishk
  • 1,873
  • 5
  • 19
  • 22
  • 1
    Possible duplicate of [Get element from within an iFrame](https://stackoverflow.com/questions/1088544/get-element-from-within-an-iframe) – Lying_cat Apr 19 '18 at 07:49

6 Answers6

208
document.getElementById('myframe1').contentWindow.document.getElementById('x')

Fiddle

contentWindow is supported by all browsers including the older versions of IE.

Note that if the iframe's src is from another domain, you won't be able to access its content due to the Same Origin Policy.

Fabrício Matté
  • 69,329
  • 26
  • 129
  • 166
25

use contentDocument to achieve this

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

var ulObj = innerDoc.getElementById("ID_TO_SEARCH");
Hary
  • 5,690
  • 7
  • 42
  • 79
8

(this is to add to the chosen answer)

Make sure the iframe is loaded before you

contentWindow.document

Otherwise, your getElementById will be null.

PS: Can't comment, still low reputation to comment, but this is a follow-up on the chosen answer as I've spent some good debugging time trying to figure out I should force the iframe load before selecting the inner-iframe element.

Pat
  • 443
  • 6
  • 13
3

You need to make sure the frame is fully loaded the best way to do it is to use onload:

<iframe id="nesgt" src="" onload="custom()"></iframe>

function custom(){
    document.getElementById("nesgt").contentWindow.document;
    }

this function will run automatically when the iframe is fully loaded.

2

In my case I was trying to grab pdfTron toolbar, but unfortunately its ID changes every-time you refresh the page.

So, I ended up grabbing it with this one-liner:

const pdfToolbar = document.getElementsByTagName('iframe')[0].contentWindow.document.getElementById('HeaderItems');

As in the array written by tagName you will always have the fixed index for iFrames in your application.

questionto42
  • 7,175
  • 4
  • 57
  • 90
Divyanshu Rawat
  • 4,421
  • 2
  • 37
  • 53
0

Apologies I don't have enough reputations to add a comment but maybe writing an answer is ok in this case. I've been looking to change the css of some html in an Iframe. I have a bookmarklet code which works outside an iframe so I'm nearly there:

javascript:(function(){ var style = document.createElement('style'), styleContent = document.createTextNode('.notifications-list--cMVsqVbKkaXV4SQ {zoom: 0.8!important;}'); style.appendChild(styleContent); var caput = document.getElementsByTagName('head'); caput[0].appendChild(style); })();

my iframe is called xm-feed-iframe

so I guessed you'd change the code to

document.getElementById('xm-feed-iframe').contentWindow.document.getElementById('x')

But I needed to work with a class in order to change the css styles. So I guess it might be as simple as changing

var caput = document.getElementsByTagName('head')

to

var caput = document.getElementById('xm-feed-iframe').contentWindow.document.getElementsByTagName('head')

so the final code would be

javascript:(function(){ var style = document.createElement('style'), styleContent = document.createTextNode('.notifications-list--cMVsqVbKkaXV4SQ {zoom: 0.8!important;}'); style.appendChild(styleContent); var caput = document.getElementById('xm-feed-iframe').contentWindow.document.getElementsByTagName('head'); caput[0].appendChild(style); })();

And hey presto it worked. I can now delve a little deeper and add some counters hopefully to the list which appears in the iframe! :)

Any comments about the code would be most appreciated as I'm self taught!

Hope this helps someone else

Thanks

UPDATE: Here we are - I know have a numbered list using a bookmarklet. My life will be so much easier now as I will no longer loss track of all the items I need to download. :D

enter image description here

Rodp
  • 31
  • 7