ATlaS ATlaS - 5 months ago 19
Javascript Question

jQuery - load html response into whole page

In my ajax code, I get a html response. How can I replace the whole page to this html response? I only find

window.location.href
, which only can redirct url response.

My code:

$('#btn').click(function(){
$.ajax({
url: '/someurl',
data: {'key': value},
type: 'POST',
success: function(html_data) {
# how to load this html_data into the whole page?
},
error: ...
});
});

Answer

jQuery replaceWith should get it done for you ;)

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

Where data is html received from server...

Entire code will look something like this...

$.get( "yourdomain.com/fileToLoad.html", function( data ) {
  $( "html" ).replaceWith( data );
});

Learn more about jQuery here...

http://jquery.com/