How to inject html into iframe, and display inside jquery-ui dialog

222 Views Asked by At

Here I create a jqueryui dialog from an iframe and populate the iframe with some html.

In firefox this displays nothing until I add the alert. Is there a better way to convince firefox to draw the iframe?

http://jsfiddle.net/jtmx00f4/6/

function fancyDialog(htm) {
    $('<iframe></iframe>').dialog({
        open: function () {
            //alert('presto!');
            var doc = this.contentDocument || this.contentWindow.document;
            doc.body.innerHTML = htm;
        }
    });
}

fancyDialog('<html><body><p>Hello</p></body></html>');
1

There are 1 best solutions below

0
On

While @dandavis fiddle does work in firefox, I also found a more complete solution in this article which does not require setTimeout.

http://jsfiddle.net/jtmx00f4/9/

function fancyDialog(htm) {
    $('<iframe></iframe>').dialog({
        open: function () {
            this.contentWindow.contents = htm;
            this.src = 'javascript:window["contents"]';
        }
    });
}

fancyDialog('<html><body><p>Hello</p></body></html>');