javascript - Get the last hash after the hash has changed - Stack Overflow

I have a backbone app where I want to open an overlay which is just a page with its own URL. You can al

I have a backbone app where I want to open an overlay which is just a page with its own URL. You can also navigate in this overlay with different pages/URLs. So when the overlay is closed I want to set back the hash to the state before the overlay was opened. As the overlay is opened by a link I can't get the hash from the state before.

So is there a way to get the previous hash when a hash changed?

I have a backbone app where I want to open an overlay which is just a page with its own URL. You can also navigate in this overlay with different pages/URLs. So when the overlay is closed I want to set back the hash to the state before the overlay was opened. As the overlay is opened by a link I can't get the hash from the state before.

So is there a way to get the previous hash when a hash changed?

Share Improve this question edited Jul 4, 2024 at 20:08 Jason Aller 3,65228 gold badges41 silver badges39 bronze badges asked Jul 18, 2012 at 13:26 Andreas KöberleAndreas Köberle 111k58 gold badges280 silver badges307 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

The hashchange event has a "oldURL" field.... store all the "oldURL" (or only the last one) and, when you need it, change the actual url with the last url.

Source: https://developer.mozilla/en/DOM/window.onhashchange

I came up with this little hack. When the the overlay is opened I store the window.history.length. When the overlay is closed I call window.history.go with the difference between the stored length and the actual and subtract 1.

var appStateActions = {
  overlayPre: function(){
    this.historyPosition = window.history.length;
  },
  overlayExit: function(){
    window.history.go(this.historyPosition - window.history.length -1);
  }
}

Unfortunately this doesn't work cause of the limit of the history. So you get a wrong result after you reach the the limit of history length.

var historyurl =[];

$(window).on('hashchange', function(e){
    historyurl.push(location.hash); 
   if(historyurl.length > 2){ 
       historyurl.splice(0,historyurl.length-2)
     }; 
});

console.log("Last Hah Url ="+historyurl[0])

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745115886a4612124.html

相关推荐

  • javascript - Get the last hash after the hash has changed - Stack Overflow

    I have a backbone app where I want to open an overlay which is just a page with its own URL. You can al

    12小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信