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


try this with jQuery :

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

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


最简单的方法是使用以下命令设置新的HTML内容:

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

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.

链接地址: http://www.djcxy.com/p/71900.html

上一篇: 如何在烧瓶背景中运行另一个python代码

下一篇: 将HTML页面替换为通过AJAX检索的内容