0

I have an ajax response, that returns the full html of a webpage, like this:

<!DOCTYPE>
<html>
  <head>
    <!-- head content -->
  </head>
  <body>
    <!-- body content -->
  </body>
</html>

So I want all of this replacing the document of my calling page after the ajax call. How do I do this?

  • I can't get that to work. The solution in that topic does not replace my HTML. –  Feb 21 '14 at 08:04

4 Answers4

1

Try this

 $('html').replaceWith(myString);
Jignesh Patel
  • 1,028
  • 6
  • 10
  • Note that this replaces the jQuery object as well, so if you're keeping references you need to update them. courtesy @Aram Kocharyan –  Feb 21 '14 at 08:14
0

use:

document.open();
document.write(myString);
document.close();

You can also try:

    var startHead = myString.indexOf("<head>");
    var endHead = myString.indexOf("</head>");        
    var headString = myString.substring(startHead+6,endHead);

    var startBody = myString.indexOf("<body>");
    var endBody = myString.indexOf("</body>");
    var bodyString = myString.substring(startBody+6,endBody);

    jQuery('head').html(headString);
    jQuery('body').html(bodyString);
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
0

I have an ajax response, that returns the full html of a webpage

If that's the case, why not just redirect to that page?

window.location.href = your_url;
xdazz
  • 158,678
  • 38
  • 247
  • 274
0

try

document.write(xmlhttp.responseText); // using AJAX
Sarath
  • 608
  • 4
  • 12