1

Here is the code that I have. Now, I want to fetch the content of nested frame. How can I achieve that?

<iframe id="frame1"> //outer frame
    <body>
        <div>Some inner contnet</div>
        <iframe id="nestedIframe"> //inner frame
            <body> //inner html goes here
                <div>Inner content of the nested frame</div>
            </body>
        </iframe>
    </body>
</iframe>
Umesh
  • 29
  • 1
  • 7
  • 1
    Try this one. http://stackoverflow.com/questions/1796619/how-to-access-the-content-of-an-iframe-with-jquery – diegoddox Jan 01 '16 at 07:30
  • It is just like adding some id to the content to the 'Some inner contnet' (refer question). But, by doing that, I was just able to get that
    itself but not the nested frame next to that.
    – Umesh Jan 01 '16 at 07:34

1 Answers1

1

Try following.

$("#frame1").contents().find("#nestedIframe").contents()

Remember that you can access IFrame content only if parent and Iframe are served by same domain, port, protocol. Otherwise it would become a CORS case. In that case if you own parent and iframe domains you can try postMessage api, as to implement postMessage you'll need to have access to both domains.

11thdimension
  • 10,333
  • 4
  • 33
  • 71
  • Okay. I'll check that out. But I have one more question. What if id of the nested frame wasn't given? How to achieve that? And yeah, I get about same domain and cross domain. That's fine with me. – Umesh Jan 01 '16 at 07:38
  • If id is not given you can use any other jQuery selectors, for example you could try `tag` selector. So the code would become `$("#frame1").contents().find("iframe").contents()` – 11thdimension Jan 01 '16 at 07:41
  • I have one more question over here. Suppose I have multiple level inner iframes, is there a way such that I get all the frame contents? Instead of navigating like ("iframe")[0].contents().("ifrmae")[1].contents()..... ? – Umesh Jan 08 '16 at 10:42
  • If you want to do it on client side, this is the only way otherwise you can try downloading the page on server and do whatever you need with it without restrictions. – 11thdimension Jan 08 '16 at 19:17
  • Okay. Thanks for the help :) – Umesh Jan 08 '16 at 20:44