views:

561

answers:

2

I have a string which looks like:

<html><head><title>example</title></head><body>some example text</body></html>

I get this string returned as a result to an AJAX request.

I would like the browser to render and display that string. The idea would be to do something like:

$('html').parent().html(myString);

Well, that doesn't work. I've attempted to use an IFRAME but I haven't figured out how to get that to work either.

Note: It is impossible for me to change this string. It is also impossible for me to regenerate this string in a subsequent call to the server (otherwise I could just redirect the browser to that url).

+4  A: 

You could just strip out the html tags, and then put everything inside the html element:

$('html').html(myString.replace(/<html>(.*)<\/html>/, "$1"));
Marius
Thanks but this doesn't work. No matter what value I try, the result of `$('html').html(somevalue)` is a blank page with an empty DOM (in IE).
Ken Browning
Ken Browning
+5  A: 

The document.open/write/close methods will do what you want:

document.open();
document.write(myString);
document.close();
Jon Benedicto
Thanks, this works perfectly. Do you happen to know if this behaves differently in any non-ie browsers?
Ken Browning
Since these have been part of the DOM for a very long time, I'd expect them to work correctly across all modern browsers.
Jon Benedicto