将当前页面数据存储在变量中,格式/结构与Ajax html响应相同

时间:2022-03-22 20:37:23

I'm storing/saving my History Ajax call responses in an array (jQuery). So on ajax load success I store the response data in an array so I can use it and apply/clone later on (for js side cache).

我在一个数组(jQuery)中存储/保存我的History Ajax调用响应。因此,在ajax加载成功时,我将响应数据存储在一个数组中,以便我可以使用它并稍后应用/克隆(对于js端缓存)。

page_cache = [];

$.ajax({
  url: url,
  type: 'get',
  dataType: 'html',
  success: function (data) {
    var $response = $(data);
    page_cache[url] = $response;
  }
});

So far so good, BUT now I need the equivalent of the ajax call response data, but on the current page so that I can stor/save it already on page load.

到目前为止这么好,但现在我需要相当于ajax调用响应数据,但在当前页面上,以便我可以在页面加载时存储/保存它。

But struggled finding a way..

但努力寻找方法..

Been trying:

page_cache[url] = $('html').data();

... but no luck..

......但没有运气..

Any suggestions?

3 个解决方案

#1


0  

If you want the entire page in the variable, you can use it:

如果您想要变量中的整个页面,可以使用它:

var $html = $('html').clone();
$html.find('body').html($response);

page_cache[url] = $html;

Regards.

#2


0  

you can try like this

你可以这样试试

var page_cache =[];
$.ajax({
  type: 'get',
  dataType: 'html',
  success: function (data) {

    page_cache[url] = data;
    $('html').html(data);
  }
});

for searching:

$('html').html(page_cache[url]);

for current page html:

对于当前页面html:

url = window.location;
page_cache[url] = $('html').html()

#3


0  

Did you tried to get the body part with $('body').html(); ?

你试图用$('body')获取身体部分.html(); ?

You can save the html with page_cache[url] = $('body').html(); and get it back with $('body').html(page_cache[url]);

您可以使用page_cache [url] = $('body')保存html .html();并用$('body')将其取回.html(page_cache [url]);

#1


0  

If you want the entire page in the variable, you can use it:

如果您想要变量中的整个页面,可以使用它:

var $html = $('html').clone();
$html.find('body').html($response);

page_cache[url] = $html;

Regards.

#2


0  

you can try like this

你可以这样试试

var page_cache =[];
$.ajax({
  type: 'get',
  dataType: 'html',
  success: function (data) {

    page_cache[url] = data;
    $('html').html(data);
  }
});

for searching:

$('html').html(page_cache[url]);

for current page html:

对于当前页面html:

url = window.location;
page_cache[url] = $('html').html()

#3


0  

Did you tried to get the body part with $('body').html(); ?

你试图用$('body')获取身体部分.html(); ?

You can save the html with page_cache[url] = $('body').html(); and get it back with $('body').html(page_cache[url]);

您可以使用page_cache [url] = $('body')保存html .html();并用$('body')将其取回.html(page_cache [url]);