简体   繁体   English

单击后退按钮时,如何返回上一个状态

[英]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.

相关问题 即使更改了哈希值,如何在网页中单击浏览器后退按钮时返回上一页? - How to go back to the previous page when browser back button is clicked in a webpage even when the hash is changed? 离子如何停止后退按钮动作不回到以前的状态或屏幕? - ionic how to stop back button action not to go to previous state or screen? 单击后退按钮时如何保留以前的HTML - How to preserve previous HTML when back button clicked 单击按钮时页面返回历史记录 - Page go back in history when button clicked 单击另一个按钮时,如何将已单击的按钮、颜色更改回原始状态 - How can i change an already clicked button, color, back to original when another button is clicked 单击浏览器后退按钮时如何维护页面状态? - How to maintain the page state when browser back button is clicked? 我如何在上一个/后退上保存状态(数据)按钮在ReactJS中单击 - How i can save state ( data ) on Previous/Back" button click in ReactJS 如何使“返回分页”按钮返回到上一个分页页面 - How to make the back to pagination button go back to previous pagination page 如何让1号按钮go点击2号按钮后变回之前的state? - How to make the button No. 1 go back to the previous state after clicking the button No. 2? 如何在单击时切换按钮颜色,并在再次单击时将颜色更改回其先前的颜色 - How to toggle button colour when it is clicked and change the colour back to its previous colour when it is clicked again
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM