[英]back button causing hash change page trap javascript
I store my webpage info in the URL hash, including page number. 我将网页信息存储在URL哈希中,包括页码。 In order to ensure the page does not exceed the total pages I added this condition. 为了确保页面不超过总页面数,我添加了此条件。 assuming total pages = 10 假设总页数= 10
//get page value from URL
function getParams(val){
return decodeURI(
(RegExp('[#|&]' + val + '=' + '(.+?)(&|$)').exec(location.hash)||[,''])[1]
);
}
var totalPages = 10;
$(window).on('hashchange',function(){
if(getParams('page') > totalPages){
window.location = "#page=1";
}
});
the problem is that if the page loads from another time where the total pages were more: 问题是,如果该页面是从另一个页面加载的,而该页面的总页面数更多:
www.website.com#page=11
it redirects to page 1, but then if the user navigates back, it goes back to page 11 for an instant then redirects back to page 1 and gets trapped in that cycle, making it impossible for the user to navigate back. 它重定向到页面1,但是如果用户向后导航,它会立即回到页面11,然后重定向回到页面1并陷入该周期,从而使用户无法导航。 is there any way to prevent this problem? 有什么办法可以防止这个问题? thanks for reading. 谢谢阅读。
I've encountered a similar problem before, use window.location.replace(url)
instead of window.location = url;
我之前遇到过类似的问题,请使用window.location.replace(url)
而不是window.location = url;
so that the previous page will not be saved in the session history. 因此上一页将不会保存在会话历史记录中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.