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:
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).
The document.open/write/close methods will do what you want:
var newDoc = document.open("text/html", "replace"); newDoc.write(myString); newDoc.close();
Unless you pass in the replace parameter, the document.open call adds page history. So users would have to click back twice to go to the previous page.
You could just strip out the html tags, and then put everything inside the html element:
At least in firefox(47.0) the solution:
var newDoc = document.open("text/html", "replace"); newDoc.write(response); newDoc.close();
does not work as suggested since pressing the back button on firefox still loads the previous history entry - i.e. the entire point of using "replace" is to avoid having users click their back button only to be greeted by the view of the page before the last time the document.write() was called. The way of doing this that does not cause the aforementioned effect is simply calling methods on the document object directly:
document.open("text/html", "replace"); document.write(response); document.close();
Another variation to try might be
document.documentElement.innerHTML = myString;
It works except for IE9.
©2020 All rights reserved.