Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace HTML page with contents retrieved via AJAX

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="...">.

like image 269
Guido Avatar asked Jan 27 '09 15:01

Guido


2 Answers

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

document.open(); document.write(newContent); document.close(); 
like image 94
niutech Avatar answered Sep 22 '22 03:09

niutech


try this with jQuery:

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

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

like image 39
shfx Avatar answered Sep 25 '22 03:09

shfx