ATTENTION ATTENTION
gear.huuah.com has launched. Visit the shop at http://gear.huuah.com/. Lots of Photo Gear at the moment
ATTENTION ATTENTION
Itsplanned.com is just launched! - Task and project management made easy. Try it for free.

Create your own lists of things to do - arrange the order to do them - move them around - group them

No limitations - all free project management - try the free demo before signing up - demo: itsplanned.com
jQuery and iframe manipulation
16.07.2009

I’ve been experimenting with some iframe-manipulation with jQuery, but this was not as easy as I have hoped for. There is not much help on jquery.com and a Google search just found others with the same kind of problem.

My setup is very basic. An iframe loading a page like this

<iframe name="framename" id="myframe" src="mypage.html"></iframe>

In the page where this iframe is being loaded, I have included the jquery.min.js and an extra .js-file for my code.


First attempt was to try the examples found on Google. For testing purposes I wanted to set a red background color on every <div> inside the iframe.

var frame = $("#myframe")[0].contentDocument;
$("div", frame).css("background-color", "red");

Nothing happens.

When testing new things I often use alert-boxes for test-output, so I tried inserting an alert-box:

alert("trying a red color");
var frame = $("#myframe")[0].contentDocument;
$("div", frame).css("background-color", "red");

And hey! When pushing the alert-box, the divs inside the iframe gets a red background-color. Or at least they do in Firefox 3.0.11 (still a no-go in IE8). Strange.

Test 2:

$("#myframe").contents().find("div").css("background-color", "red");

Nothing! Blank!

Oh well. Why not try the alert-box again:

alert("trying a red color");
$("#myframe").contents().find("div").css("background-color", "red");

Hmm.. this seems to work in both Firefox 3.0.11 and Internet Explorer 8 IF you don’t push the alert-box until the iframe is finished loading.

So perhaps it’s all about not accessing the iframe before the contents has been loaded. So I tried wrapping the whole thing in a ready-function like this:

$("#myframe").ready(function () {
...
}

That did not help at all.

Well. Google google google. And bingo. There’s a plugin for jQuery called frameReady and this does the trick.

Third and final test:
Include the jQuery.frameReady.js in the main page and enter this code:

$.frameReady( function() {
    $("div").css("background", "red");
  },
  "framename"
);

This loads the iframe and sets a red background color on every div inside the iframe. Tested in IE8 and Firefox 3.0.11.

Conclusion:
Use the frameReady plugin for jQuery when manipulating with iframes.

Update: If you’re having trouble getting this to work, please check the source code for frameReady and verify that the hardcoded path for /js/jQuery.js matches your site configuration!

10 Responses to “jQuery and iframe manipulation”

  1. Giraldi Maggio Says:

    Is this restricted to local iframes only or does it also work with childpage at another hostname as well?

  2. js - huuah Says:

    I did some testing on this, and as far as I can remember, I saw some differrent results in Internet Explorer and Firefox. If you include jQuery on both the “parent” and the “child” page on the same domain, the problems aren’t as big as when the iframe is located on a different hostname.

  3. Vertical Grain Says:

    I’m also curious about whether there are ways to get frameReady to work when the childpage is from a different hostname. This works for me when the childpage and parent page are on the same host but not when the childpage is different host.

  4. nau Says:

    what if the iframe does not have a name or id?

  5. Muzi Says:

    Thanks for this, it was very helpful

  6. André Banderas Says:

    Sorry, my english is terrible, I write in pt-br!! :(

    Bom, você pode tentar usar:

    var frame = $(“#myframe”).get(0).contentDocument.body;
    $(“div”, frame).css(“background-color”, “red”);

    Até

  7. js - huuah Says:

    #5 André
    I’m sorry, but I do not understand your question

  8. Emanuele DG Says:

    Hello,
    I used jQuery to acheve this task and it worked.
    Example:

    var iframe = $('<iframe></iframe>').attr('id', 'blabla').attr('name', 'bla-bla').attr('src', 'about:blank');
    iframe.load(function() {
    	$(this).contents().find('body').eq(0).append($('<div>Hello world!</div>'));
    });
    $('#myDiv').append(iframe);
    
  9. Peter soski Says:

    Im just tryoing to see content on my left frame when i mouse over theimages on my right frame…
    http://bobsob.com/lr.htm
    Normally it worked fine: http://bobsob.com/bt2.htm

    HELP!

  10. Soso Says:

    Now, you can use the contents API of Jquery but this won’t work on different domains!
    http://api.jquery.com/contents/

Leave a Reply