How to select and replace whole page using jQuery

My page design makes me refresh the whole page with the html that I downloaded via ajax.

$('html').replaceWith(data);

Gives me errors. Any ideas?

+6
jquery jquery-selectors css-selectors
source share
4 answers

Use body:

 $('body').replaceWith(data); 
+17
source share

I had the same problem, but that did not help. If you also need to replace the <head> (so, the whole page), you can also do

 document.write(newPage); 
+17
source share

I had problems with

 $("body").replaceWith(newPage) 

giving me some weird CSS issues, but that was fine:

 $("body").html(newPage); 
+8
source share

Strange behavior of jQuery.replaceWith and jQuery.html when executed using the body selector. You will lose the body tag after calling:

 $('body').replaceWith('<body>New body</body>'); 

This does not happen with any other selector, like:

 $('title').replaceWith('<title>New title</title>'); 

Also jQuery.html does not double the body tag (like other tags) and works like replaceWith when called like this:

 $('body').html('<body>New body</body>'); 

Hope this is not a jQuery gray zone. Or, if so, they do not think to correct it. I have applications in which I use $ ('body'). Html when $ ('body'). ReplaceWith should be used.

+2
source share

All Articles