gpt4 book ai didi

javascript - 使用 ajax 时监听浏览器后退按钮并将 html 数据保存为对象。是好是坏?

转载 作者:数据小太阳 更新时间:2023-10-29 04:19:46 24 4
gpt4 key购买 nike

我正在创建一个脚本来监听浏览器后退按钮并加载上一页。我是 jquery 和 javascript(一个 php 人)的新手。在重新发明它之前,我在整个网络上搜索了一个图书馆。但是因为我在我的 ajax 链接中使用了很多参数,所以我不能使用那些库。我承认这是我的错误,因为我不知道如何使用如此复杂的系统。所以我在考虑是否创建一个系统如下。

// get the contents of a particular div and save as an object/associative array 
// { hash : pageNumber, html : content}
function save_history(div){
var content = $(div).html();
// increment the page number and add hash tags to the URL
}

// Listen to the browser hash value change
$(window).bind('hashchange', function () {
hash = window.location.hash;
if(hashValue){
load_history(hashValue);
}
});

// Load data from history
function load_history(id){
// fetch the content based on the hashvalue
$(div).html(content);
}

使用这个有什么问题吗?当大量内容保存为对象时,这会导致页面无响应或浏览器崩溃吗?如果它导致此类问题,我不想浪费我的时间。

最佳答案

似乎 pjax 是您正在寻找的库:https://github.com/defunkt/jquery-pjax

关于javascript - 使用 ajax 时监听浏览器后退按钮并将 html 数据保存为对象。是好是坏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12359895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com