How to access iframe in jQuery

Assuming you have

1
  <iframe id="iframeID" ...></iframe>

Iframe contains div with id=”someID”:

1
  <div id="someID">Hello world!</div>

Need get div’s text?

1
  $('#iframeID').contents().find('#someID').html();

This entry was posted on Friday, March 21st, 2008 at 21:45 and is filed under jQuery. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.





42 Responses to “How to access iframe in jQuery”

Thanks, this was a big help. Works for me!

You’re welcome, jGeek! :)

… with this kind of thing, is’nt it possible to steal other peoples email, etc … (?)

Rog, what you mean?

i must be wrong somehow … but, this is what I thought when i read your tip :: i deliver a web page, with an iframe in it with src of mail.google.com. i hide the iframe somewhere/how, and using the jquery code, read the content and post the interesting stuff back to my original server … (?)

This didn’t work for me. One thing you must keep in mind is that just because the parent page is loaded or ready, doesn’t mean the frame is done. Here’s what i got to work:


[code]

jQuery and Frames

jQuery( function() {
$(’#frame1′).load( function(){
$(this.contentDocument).find(’body’).html(’This frame was modified with jQuery! Yay!!!’)
});
$(’#frame2′).load( function(){
$(this.contentDocument).find(’body’).html(’This frame was modified with jQuery! Yay!!!’)
});
});

jQuery and Frames

Frame from the same domain as parent.

Frame from a different domain as parent.

[code]

rog, good thinking… but browsers follow similar rules with iframes as they do with xml_http_requests: that is, it blocks cross-domain access.

For example, if I am running the above script from the page: http://www.liveintensely.com/parent.html, I can read and write to the frame which’ source is http://www.liveintensely.com/frame1.html. HOWEVER, if the frame’s source is on a different domain, like http://www.google.com, javascript is denied both read and write access to the frame.

Wow… didn’t know html came through when posting. Please excuse the double post.

This didn’t work for me. One thing you must keep in mind is that just because the parent page is loaded or ready, doesn’t mean the frame is done. Here’s what i got to work (the first example):


<html>
    <head>
        <title>jQuery and Frames</title>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
            jQuery( function() {
                $('#frame1').load( function(){
                    $(this.contentDocument).find('body').html('This frame was modified with jQuery! Yay!!!')
                });
                $('#frame2').load( function(){
                    $(this.contentDocument).find('body').html('This frame was modified with jQuery! Yay!!!')
                });
            });
        </script>
    </head>
    <body>
        <h2>jQuery and Frames</h2>
       
        <h4>Frame from the <i>same</i> domain as parent.</h4>
        <iframe id="frame1" src="iframe.html"></iframe>
       
        <h4>Frame from a <i>different</i> domain as parent.</h4>
        <iframe id="frame2" src="http://google.com"></iframe>
    </body>
</html>

rog, good thinking… but browsers follow similar rules with iframes as they do with xml_http_requests: that is, it blocks cross-domain access.

For example, if I am running the above script from the page: http://www.liveintensely.com/parent.html, I can read and write to the frame which’ source is http://www.liveintensely.com/iframe.html. HOWEVER, if the frame’s source is on a different domain, like http://www.google.com, javascript is denied both read and write access to the frame.

thanks for your reply. i couldn’t imagine that this would be possible - but it’s good to know exactly the mechanics of how it works … thanks.

Good work, very thanks.

Нема за що ))

Mike, I’m quite a jQuery noob, but:

* shouldn’t it be $(’#frame1′).ready() instead of $(’#frame1′).load() ? “load” doesn’t seem to trigger anything (but no errors either).

* doesn’t $(this.contentDocument) refer to the current document instead of the iframes?

you have no idea how invaluable this tidbit of knowledge has been for me

A reverse Proxy should be the right thing for your Problem. @rog

Renatogarou

Hello !
Can I use this method for acess the content of Tinymce ? How ?

Sorry my English .

Renatogarou, I think you can use this with TinyMCE, but I’m not familiar with it so unable to provide more help… Probably you can find information you’re looking for in TinyMCE API documentation.

Hi fellas.
How can I pass param(s) to load handler?

thanks a lot. it works

_ts_,
what you mean?

roberto,
you’re welcome :)

2 Taras
Thank you. I’ve already found the solution for my question.

But I’ve another one.

if iFrame has some function named as “someFunction” and how can I call this function from parent window?

Thank you!

sure… just:
[code]
frameId.someFunction(params);
[/code]

(assuming )
at least that works in firefox 3.0.10…

cheers, matt

Hi, I’ve looked at the code suggested by Taras and Mike; thanks guys. But I found that neither of them work very well. Jquery converts HTML objects into object objects, which restricts the abilities of manipulating iframes. I found that a better approach to address an iframe is using the best of both worlds:

var iframeRef = document.getElementById(’iframeID’);
$(iframeRef).find(’html’).html(’your content here’);

Taras hey! Thank you very much!!
this tiny bit of information saved me hours of working. ^^

2 Nate:
I think, for jQuery way we can get

var iframeRef = document.getElementById(’iframeID’);

by simple:

var iframeRef = $(’#iframeID’);

Thankyou so much, How about iframe 2 level deep (get value from iframe inside iframe).

Best regards,

hi
I’m trying to add script tag to the Head of the Iframe but it doesn’t work

can you help me with that, this is my code

$(iframe).contents().find(’head’).html(”
+ ”);

the css file included correctly but the script tag no
any help

Thanks a ton for posting this. I didn’t read the comments but I’m sure somewhere up there, someone is trying to prove that they’re smarter than you…just the nature of being a web developer I suppose. Just know that I’m sure this little piece of code probably saved someone their job at some point…me, just saved me from losing another day of work trying to figure this out, lol.

if in reverst,

how can i out the value from the abc.com into “list”

thanks,

if in reverst,
div id=list /div

iframe src=abc.com

how can i output the value from the abc.com into “list”

thanks,

Thanks !

Good stuff here. My question is how can a script embedded in the document within the iframe close the entire jquery dialog which was called from the parent?

I have done so much in Jquery and have learned most tricks of the trade but this is probably the best thing I have ever come across. I am so excited about the endless posibilitys that this will bring. Thanks sooooooooo much for sharing!

Thaks! thats help me and work for me.

This is not working for me. Please help me for this. I am posting the jquery code here

the id of iframe is #depGridFrame

$(’#depGridFrame’).ready(function()
{
alert($(this).contents().find(’#updtGrid’).tagName);
});

i am calling it from document.ready function of parent document.

These iframes are getting on my nerves. I can’t get it right …

If anyone is still having trouble with selecting iframe contents w/jQuery I’ve verified this as working (keep in mind the iframe must be in the same domain as the accessing page, as mentioned above):

var myFrame = $(’#myFrameId’);
myFrame.load(function() { // wait for iframe to load
myFrame.contents().find(’#myElementInFrame’).html(’hello world!’);
});

This was a very big help.
i spent hours trying to maniuplate the iframe.
thanks a lot

@Taras, @cbsides Thank you very much got it working

Karl Stanton

I found this very helpful, thank you!!

thanx a lot bro, i found this ..

hello all
what to do if the domain is different ?

i want a jquery which will hide some of the div from the the website in the iframe from the different domain.

This work for me.
$(’#iframe-name’).contents().find(’#frame-do-you-want-get’)

Example:
$(’#TB_iframeContent’).contents().find(’#wrapper’).css(’display’,'block’);

Thanks bro.

Leave a Reply


required


required (will not be published)



CAPTCHA image