Replace HTML page with contents retrieved via AJAX

JavascriptHtmlAjaxDom

Javascript Problem Overview


I have an HTML page with a typical structure:

<html>
  <head>
   <script src="..." ></script>
   <style>...</style>
  </head>
  <body>
   content
  </body>
  <script>
    var success_callback = function(data) {
      // REPLACE PAGE CONTENT & STRUCTURE WITH "data"
    }
    ajax(url, params, success_callback);
  </script>
</html>

Do you think it is possible ? I've already tried to give the html tag an id and doing $(id).replace(data); with no success.

Don't ask me why, but that is what I need (I'm working with a special "mashup builder" site... it is a long story).

EDIT : I forgot to say that scripts in the received content have to be executed, even external scripts included using <script src="...">.

Javascript Solutions


Solution 1 - Javascript

The simplest way is to set the new HTML content using:

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

Solution 2 - Javascript

try this with jQuery:

$('body').load( url,[data],[callback] );

Read more at docs.jquery.com / Ajax / load

Solution 3 - Javascript

Here's how to do it in Prototype: $(id).update(data)

And jQuery: $('#id').replaceWith(data)

But document.getElementById(id).innerHTML=data should work too.

EDIT: Prototype and jQuery automatically evaluate scripts for you.

Solution 4 - Javascript

You could try doing

document.getElementById(id).innerHTML = ajax_response

Solution 5 - Javascript

the simplest way is

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

Solution 6 - Javascript

Can't you just try to replace the body content with the document.body handler?

if your page is this:

<html>
<body>
blablabla
<script type="text/javascript">
document.body.innerHTML="hi!";
</script>
</body>
</html>

Just use the document.body to replace the body.

This works for me. All the content of the BODY tag is replaced by the innerHTML you specify. If you need to even change the html tag and all childs you should check out which tags of the 'document.' are capable of doing so.

An example with javascript scripting inside it:

<html>
<body>
blablabla
<script type="text/javascript">
var changeme = "<button onClick=\"document.bgColor = \'#000000\'\">click</button>";
document.body.innerHTML=changeme;
</script>
</body>

This way you can do javascript scripting inside the new content. Don't forget to escape all double and single quotes though, or it won't work. escaping in javascript can be done by traversing your code and putting a backslash in front of all singe and double quotes.

Bare in mind that server side scripting like php doesn't work this way. Since PHP is server-side scripting it has to be processed before a page is loaded. Javascript is a language which works on client-side and thus can not activate the re-processing of php code.

Solution 7 - Javascript

I'm assuming you are using jQuery or something similar. If you are using jQuery, then the following should work:

<html>
<head>
   <script src="jquery.js" type="text/javascript"></script>
</head>
<body>
   content
</body>
<script type="text/javascript">
   $("body").load(url);
</script>
</html>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGuidoView Question on Stackoverflow
Solution 1 - JavascriptniutechView Answer on Stackoverflow
Solution 2 - JavascriptshfxView Answer on Stackoverflow
Solution 3 - Javascriptgeowa4View Answer on Stackoverflow
Solution 4 - JavascriptThe.Anti.9View Answer on Stackoverflow
Solution 5 - JavascriptLopesDaLesteView Answer on Stackoverflow
Solution 6 - JavascriptxaddictView Answer on Stackoverflow
Solution 7 - JavascriptMariusView Answer on Stackoverflow