0

Here is the initial HTML, it is fairly small. It's only meant to be the loading screen.

<html>
  <head>
    <title>Simple Hello World</title>
    <link href="rapid-ui.css" type="text/css" rel="stylesheet" />
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript" src="rapid-ui.js"></script>
  </head>
  <body>
   This is the body of the page.
  </body>
</html>

The "rapid-ui.js" will generate an HTML string, this will be a full document - including DOCTYPE, head, meta, scripts, css, etc. After it is done generating the HTML it should then display that HTML on top of the page in an iframe in the same way as if the "src" of that iframe was set to a URL that generated that same HTML string.

I tried this:

function showHTML(html) {
  var iframe = $('<iframe id="display" frameborder="0"></iframe>');
  iframe[0].src = 'data:text/html;charset=utf-8,' + encodeURI(html);
  iframe.appendTo('body');
}

It doesn't seem CSS or scripts are executed properly.

This doesn't work either

function showHTML(html) {
  var iframe = $('<iframe id="display" frameborder="0"></iframe>');
  iframe[0].innerHTML = html;
  iframe.appendTo('body');
}

3 Answers 3

2

You cannot just insert data into an iframe before it is appended to the dom. Try this

function showHTML(html) {    
    $('<iframe id="display" frameborder="0">').appendTo('body')
                              .contents().find('body').append(html);
}

Here is another way if you need to edit the head.

function showHTML(html) {    
    var iframe = $('<iframe id="display" frameborder="0">').appendTo('body');
    var doc = iframe[0].contentWindow.document;
    doc.open();
    doc.write(html);
    doc.close();
}
Sign up to request clarification or add additional context in comments.

4 Comments

This is very close - the CSS and HTML are inserted properly - but DOCTYPE and meta are not respected. Also scripts are not evaluated.
Edited it to make it respect all parts of the document
Oh nevermind - let me investigate more - i think maybe the problem in IE 7/8 is happening in my HTML generation code is resulting in different HTML somehow.
Yes - I have now verified - the problem in my code was with the HTML generation - I have fixed that and now all is good with IE 7/8
0

I found from here: Creating an iframe with given HTML dynamically a possible solution

function showHTML(html) {
    var iframe = $('<iframe id="display" frameborder="0"></iframe>').appendTo('body');
    var doc = iframe[0].contentWindow.document;
    doc.open();
    doc.write(html);
    doc.close();
}

This solution seems to work.

Comments

0

Try this:

function showHTML(html) {
    var iframe = '<iframe id="display" frameborder="0"></iframe>';
    $("body").append(iframe).append(html);
}

append will append within the element

If you need to edit the src of the iframe instead... do this:

function showHTML(html) {
    var iframe = '<iframe id="display" frameborder="0"></iframe>';
    $("body").append(iframe).attr("src", "data:text/html;charset=utf-8," + encodeURI(html));
}

or this:

function showHTML(html) {
    var fSrc = "data:text/html;charset=utf-8," + encodeURI(html);
    var iframe = '<iframe id="display" src="\" + fSrc + "\" frameborder="0"></iframe>';
    $("body").append(iframe);
}

1 Comment

The above code is wrong in many ways. You are doing nothing with the iframe variable.. $('iframe') searches the DOM, not in memory elements. $('iframe').append(html) will not work for iframes

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.