[英]How can I go back to the previous state when back button is clicked
I am using the history.pushState
to change the content of a page with ajax
request. 我使用
history.pushState
来更改带有ajax
请求的页面内容。
And its very certain that after a history.pushState
, the forward button is disabled in the browser so definitely any button that would be clicked will be the back button. 并且非常肯定在
history.pushState
,在浏览器中禁用了前进按钮,所以肯定任何点击的按钮都是后退按钮。
So I want to go back to the main state of the page before the pushState. 所以我想在pushState之前回到页面的主要状态。 I use ajax request to retrieve the contents of the main state but the problem is that how can I make the url in the browser go back to the main url.
我使用ajax请求来检索主状态的内容,但问题是如何让浏览器中的url返回主url。 I don't want to use the pushState method for this due to some issues
由于某些问题,我不想为此使用pushState方法
I increment a value by one after ever ajax success in other to know how far I have pushState. 在ajax成功之后,我将一个值增加一个,以便知道我有多远pushState。
var mainState;
var pushed = false;
$('a').click(function(){
$.ajax({
url: "/path/to/content",
success: function(data){
$('div').html(data);
history.pushState('state', 'title', 'url');
mainState ++;
pushed = true;
}
})
})
window.onpopstate(function(){
if(pushed){ //knowing fully well that the forward button is disabled
// take it back to the main state
history.go(-mainState);
}
})
Let's say the mainState is 3 and I click the back button instead of going to the mainState it takes me to mainState - 3. 假设mainState为3,我单击后退按钮而不是转到mainState,它将我带到mainState - 3。
Please anyone know I can do this? 请有人知道我可以这样做吗?
var mainState = 3;
-mainState; //-3
--mainState; //2
Your code should look like this 您的代码应如下所示
window.onpopstate(function(){
if(pushed){
history.go(--mainState); //FIXED
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.