Javascript 使浏览器删除特定的历史状态

Javascript 使浏览器删除特定的历史状态,javascript,jquery,html,history.js,Javascript,Jquery,Html,History.js,我有一个ajax控制的网站,我有两种类型的页面,显示给用户。为了简单起见,我们将它们称为主页s和子页面s主页s包含信息,子页面s都是表单,例如,用户可以在其中添加或修改主页的现有信息 如果我的网站被使用兼容HTML5浏览器的用户访问,我会在他/她浏览我的网站时使用HistoryJS更新url。让我们按以下示例: 用户进入我的网站并按以下顺序导航到以下页面,其历史记录如下所示: 主页(1)-->主页(2)-->子页(1)-->子页(2) 当用户在子页面(2)上完成表单时,我想立即将他重定向到他访问

我有一个ajax控制的网站,我有两种类型的页面,显示给用户。为了简单起见,我们将它们称为主页s和子页面s主页s包含信息,子页面s都是表单,例如,用户可以在其中添加或修改主页的现有信息

如果我的网站被使用兼容HTML5浏览器的用户访问,我会在他/她浏览我的网站时使用HistoryJS更新url。让我们按以下示例:

用户进入我的网站并按以下顺序导航到以下页面,其历史记录如下所示:

主页(1)-->主页(2)-->子页(1)-->子页(2)

当用户在子页面(2)上完成表单时,我想立即将他重定向到他访问的最后一个主页。例如,当用户完成表单时,我希望用户历史记录如下:

主页(1)-->主页(2)

在视觉上,我能够做到这一点,一切正常,但在HTML5浏览器中,如果我按下浏览器上的本机后退键,页面将尝试还原到子页面(1),即初始历史中正确的后退状态

删除一些历史状态是否可行?如果可以,我该怎么做?

以下是我目前使用的代码:

ConverserNavigation.prototype.getPreviousMainAction = function() {
 // NOTE: the code that deals with non HTML5 compatible browsers, 
 // was removed because everything works fine there
 var startFrom, found=false;

 if (this.HistoryJS.status==true) startFrom=History.getState().data.id;    
 // if browser is HTML5 compatible, get the current state from History object, 
 // which is a HistoryJS object

 while ((!found) && (startFrom>0)) // find the last MAINPAGE visited by user
 {
     startFrom--;
     if (this.historyData[startFrom].pageData.page != 'quickactions') found=true;   
 }



 if (this.HistoryJS.status==true) History.replaceState({id:startFrom}, this.historyData[startFrom].urlData.title, this.historyData[startFrom].urlData.url);
 // replace the current history state, with the one to where we want to revert

 this.currentNavigationId=startFrom;
 this.back(); // render the ui to navigate back to the previous page, works as intended
 for (var i=this.currentNavigationId;i<this.historyData.length;i++) delete this.historyData[i]; // delete the unused history data

}
ConverserVigation.prototype.getPreviousMainAction=函数(){
//注意:处理非HTML5兼容浏览器的代码,
//被移除是因为那里一切正常
var startFrom,found=false;
如果(this.HistoryJS.status==true)startFrom=History.getState().data.id;
//如果浏览器兼容HTML5,则从历史对象获取当前状态,
//哪个是HistoryJS对象
while((!found)&&(startFrom>0))//查找用户访问的最后一个主页
{
从--;
如果(this.historyData[startFrom].pageData.page!=“quickactions”)找到=true;
}
如果(this.HistoryJS.status==true)History.replaceState({id:startFrom},this.historyData[startFrom].urlData.title,this.historyData[startFrom].urlData.url);
//将当前历史状态替换为要还原到的状态
此.currentNavigationId=startFrom;
this.back();//呈现ui以导航回上一页,按预期工作

对于(var i=this.currentNavigationId;i我通过以下方式修改代码来解决此问题:

替换此行:

if (this.HistoryJS.status==true) History.replaceState({id:startFrom}, this.historyData[startFrom].urlData.title, this.historyData[startFrom].urlData.url);
为此:

if (this.HistoryJS.status==true) {
    History.go(goBack); //goBack is the number of states I have to backtrack
    this.HistoryJS.manualStateChange=false; // telling the browser to not fire my own UI updating functions
    History.back(); // navigating one more state back in the History object
    History.pushState({id:startFrom}, this.historyData[startFrom].urlData.title, this.historyData[startFrom].urlData.url); // recreating the original state in the History.
    this.HistoryJS.manualStateChange=true; // restarting the UI update functions on pop or push events
}