3

Is there a way to perform an AJAX GET on a page and retrieve the final version of the HTML on that page after all of its own scripts have finished adjusting its contents?

Right now if I perform a GET on a page with scripts that are adding to the HTML, it'll give me the full HTML as it was before that extra content was added.

So if there's a javascript generating a specific table on that page, it wouldn't be returned by the GET. Because it didn't exist before that page's scripts generated it.

How can you get() the finished product of a page's HTML, after anything on that page altered it?

4

1 回答 1

2

AJAX 调用只会返回服务器发回的内容,但是您可以做的是在 DOM 中删除响应(数据),这将导致 javascript 运行,然后您可以使用 jQuery 取出内容, 请参见下文:

$.get("data.html",function(data){
  //insert the response in a hidden div that will cause js to run and apply changes
  var $container = $("<div>").html(data);
  var newData = $container.html();
  //newData will hold the final html 
});

注意:实际script元素不会在newData.

希望这可以帮助

于 2013-07-22T21:33:36.880 回答