Javascript 返回到ajax结果页面上最后一次看到的位置
对于我的项目,我希望能够点击后退按钮并返回到页面中我上次所在的位置。首先,发生了什么?用户位于一个搜索页面上,该页面每次点击页面底部时都会加载50个结果 发生了什么: 我有一个搜索页面,首先显示50个结果,然后在用户到达页面底部时通过ajax调用加载接下来的50个结果 问题是: 当用户返回浏览器时,他们希望被定位在上次离开的页面上 我想做的是: 首先,Javascript 返回到ajax结果页面上最后一次看到的位置,javascript,jquery,ajax,Javascript,Jquery,Ajax,对于我的项目,我希望能够点击后退按钮并返回到页面中我上次所在的位置。首先,发生了什么?用户位于一个搜索页面上,该页面每次点击页面底部时都会加载50个结果 发生了什么: 我有一个搜索页面,首先显示50个结果,然后在用户到达页面底部时通过ajax调用加载接下来的50个结果 问题是: 当用户返回浏览器时,他们希望被定位在上次离开的页面上 我想做的是: 首先, $(window).on('beforeunload', function () { var lastScrollPosition =
$(window).on('beforeunload', function () {
var lastScrollPosition = $(window).scrollTop() + $(window).height();
document.cookie = "lastPageLoadCount=" + lastPageLoadCount + ";";
document.cookie = "lastScrollPosition=" + lastScrollPosition + ";";
//$(window).scrollTop(0);
});
当用户离开搜索页面时,我会保存他们在页面上的位置以及ajax被触发多少次的cookies。我认为在进入下一页之前滚动到顶部是有意义的,但这似乎给了我一个不正确的滚动位置
var lastPageLoadCount = 0;
$(document).ready(function () {
var prevLastPageLoadCount = getCookie("lastPageLoadCount");
if (prevLastPageLoadCount != '') {
// below is where the problem starts
while (lastPageLoadCount < parseInt(prevLastPageLoadCount)) {
// manually triggering the ajax call here
loadMoreResults(); // increment lastPageLoadCount when ajax returns success
}
// then scroll down into position
// ...
// then delete this cookie
document.cookie = "lastPageLoadCount=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
}
当用户回击返回搜索结果时,我希望读取这些cookie并执行必要的操作来恢复页面。如果用户之前两次触发ajax,那么我希望在尝试向下滚动到正确位置之前自动再次触发ajax
var lastPageLoadCount = 0;
$(document).ready(function () {
var prevLastPageLoadCount = getCookie("lastPageLoadCount");
if (prevLastPageLoadCount != '') {
// below is where the problem starts
while (lastPageLoadCount < parseInt(prevLastPageLoadCount)) {
// manually triggering the ajax call here
loadMoreResults(); // increment lastPageLoadCount when ajax returns success
}
// then scroll down into position
// ...
// then delete this cookie
document.cookie = "lastPageLoadCount=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
}
var lastPageLoadCount=0;
$(文档).ready(函数(){
var prevLastPageLoadCount=getCookie(“lastPageLoadCount”);
如果(prevLastPageLoadCount!=''){
//下面是问题的起点
while(lastPageLoadCount
这并不完全有效。似乎在页面准备就绪之前就已经命中了循环,这没有任何意义。虽然可能会无休止地调用while循环,但ajax似乎没有发挥作用。我有足够的经验告诉我setinterval、settimeout或回调是否会对我有所帮助。总体而言,这似乎是一个非常重要的问题坏主意
:(
setCookie({id:my unique page name,offset:0,limit:50})
)SELECT*FROM blah WHERE blah LIMIT 0,50
),下一步将是SELECT*FROM blah WHERE blah LIMIT 50,100
等等//当ajax返回成功时增加lastPageLoadCount
这是没有用的。只需从第2点开始跟踪它while(
这是不需要的 defaultLimit = 50, defaultOffset = 0;
on(beforeWindowUnload){
setCookie({ DOMOffsetPosFromCoockie : ($(window).scrollTop() + $(window).height()) });
}
// when user click more results
if (userClickMoreResults) {
var offset = offsetFromCoockieNotSet ? defaultOffset : offsetFromCoockie;
var limit = limitFromCoockieNotSet ? defaultLimit : limitFromCoockie;
setCookie({
id: myUniquePageName,
// if your code is designed to load results not replacing the previous loaded content
// ( ex.: you load all the results from 0 to a max viewed in a certain moment)
// offset: offset + 50, otherwise offset is always 0
offset: offset,
limit: limit + 50
});
}
// when page load
if (myUniquePageName == myUniquePageIDFromCoockie){
if (limitFromCoockieNotSet && offsetFromCoockieNotSet) {
loadAjaxResults(defaultLimit, defaultOffset);
} else {
loadAjaxResults(limitFromCoockie, offsetFromCoockie);
}
setInterval(function(){
$('html, body').animate({scrollTop: DOMOffsetPosFromCoockie}, 2000);
},200);
}
你能提供代码的其他部分吗?很抱歉,这不能回答问题。@Peter Mercury:请查看更新的答案并让我知道。