Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I replace the entire HTML node using jQuery

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).

like image 247
Ken Browning Avatar asked Aug 06 '09 00:08

Ken Browning


People also ask

How do you replace an entire HTML?

Use . replace() method on HTML element and replace it with the new HTML Document(eg.. $('html').

How can you update an HTML element using jQuery?

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.

How do you replace an element with another in jQuery?

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.

How can I replace all characters in a string using jQuery?

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.


1 Answers

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.

like image 176
Jon Benedicto Avatar answered Oct 09 '22 11:10

Jon Benedicto