0

I have an empty frame in my page, with src set to '' (empty). I also have a string like this:

<html>
  <head>
    <base src="http://www.example.com">
    <title>Blah</title>
  </head>
  <body>
    <p>Fancy web page</p>
  </body>
</html>

I would like to populate the iframe with the string, so that the exact content of it matches the string.

What's the best way to do this? I am going insane trying to do it.

Merc
  • 16,277
  • 18
  • 79
  • 122
  • Probably this can help you - http://stackoverflow.com/questions/6102636/html-code-as-iframe-source-rather-than-a-url – Kunj Apr 09 '14 at 05:25

1 Answers1

0

You need to set an id on the iframe and then set the innerHTML of the iframe's document body and head to the head/body string values.

Here's a working fiddle: http://jsfiddle.net/theoutlander/dw3r7/1/

<iframe id=testframe></iframe>

var strHead = '<base src="http://www.example.com"><title>Blah</title>'
var strBody = '<p>Fancy web page</p>';

testframe.document.head.innerHTML = strHead;
testframe.document.body.innerHTML = strBody;
Nick
  • 4,002
  • 4
  • 30
  • 41
  • My input doesn't come in two pieces. Plus, body might well have attributes which would be lost. The point of my question is that I have a whole string that represents the whole page... – Merc Apr 09 '14 at 12:28
  • Hmm. I guess my solutions are hacky for what you want. What about posting the string to a server instead? You could serve it as a resource in that case and have the iframe point to that location? I need to ask what you're trying to do here? Why do you have a page as a string? – Nick Apr 10 '14 at 06:26