9

I am filling an empty iframe with basic HTML, using $iframe.contents().find('body').html(contentBody);

See: http://jsfiddle.net/UjT2b/2/

This works well on Chrome. On Firefox, I can see very briefly the content inside, but then it suddenly disappears. When I set a breakpoint on this line with Firebug, then continue running, the content stays inside. But if I set a breakpoint on the line after, it goes away.

Any clue on how to fix this?

5
  • Is that the right fiddle? I'm not seeing any thing that has to do with an iframe. Apr 1, 2012 at 19:55
  • Ok, I'm getting the same result for Chrome, Safari and Firefox. I'm using Firefox 11 on Mac. Could it be something else? Apr 1, 2012 at 20:02
  • The fiddle works. But I'm doing exactly the same thing in my app and it doesn't. And the weird thing is that a breakpoint solves the problem. So I have no clue on how to debug this.
    – Clément
    Apr 1, 2012 at 20:09
  • Okay, it seems like putting the line in a setTimeout (even with 1 ms) fixes the problem. I would still like to understand why though.
    – Clément
    Apr 1, 2012 at 20:19
  • 3
    stackoverflow.com/questions/7828502/… looks like it might be pertinent. Apparently if you put the change in before the document finished loading, firefox overwrites with a blank at the end of the load. Document.onload will likely serve you better than a timeout, if this is what is going on.
    – Ben Barden
    Apr 2, 2012 at 14:46

2 Answers 2

7

I have encountered similar problem while attempting to fill dynamically created iframe. Using of iframe onload event solved the situation for me. As seen the onload solution does not work for other browsers than FF, hence the preserved standard way.

/**
 * Fills an iframe using data stored within textarea. Useful for creating email
 * template previews
 *
 * @param  {String} inputSelector
 * @param  {String} outputElemClasses
 * @return void
 */
function displayEmail(inputSelector, outputElemClasses)
{
    $(inputSelector).each(function(i) {
        var templateData = $(this).text();
        var $iframe = $('<iframe></iframe>');
        $iframe.addClass(outputElemClasses);
        $iframe.insertAfter(this);
        // non-firefox
        updateIframe($iframe, templateData);
        // firefox
        $iframe.load(function(e){
            updateIframe($iframe, templateData);
        })
    })
}


/**
 * Fills in target iframe using supplied data
 *
 * @param  {Object} $iframe
 * @param  {String} data
 * @return void
 */
function updateIframe($iframe, data)
{
    $iframe.contents().find('html').html(data);
}            
1
  • 1
    this solved it for me. appears to be the correct answer.
    – chevett
    Nov 23, 2022 at 22:02
2

I cannot see your entire code, but have worked with what you gave me in a way that I would do it. There is a chance that you are not giving the script the chance to let the rest of the HTML load before it is executed and thus unpredictable things can happen. I find that if I do not wait for the window to load then the image will not show up. If you encase everything you did, like below, then you shouldn't have a problem:

$( window ).load( function() {

    var html = "<img src='http://www.google.fr/logos/2011/Louis_Daguerre-2011-hp.jpg' alt='image' />";


    $( "#myIframe" ).contents().find( 'body' ).html( html );

});

Just remember that jQuery.html() will replace all the HTML inside the element given.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.