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).
Use . replace() method on HTML element and replace it with the new HTML Document(eg.. $('html').
We can replace HTML elements using the jQuery . replaceWith() method. With the jQuery replaceWith() method, we can replace each element in the set of matched elements with the provided new content and return the set of elements that were removed.
To replace a DOM element with the specified HTML or DOM elements using jQuery, use the replaceWith() method. The replaceWith (content) method replaces all matched elements with the specified HTML or DOM elements. This returns the JQuery element that was just replaced, which has been removed from the DOM.
The replaceAll() method is an inbuilt method in jQuery which is used to replace selected elements with new HTML elements. Parameters: This method accepts two parameter as mentioned above and described below: content: It is the required parameter which is used to specify the content to insert.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With