如何使浏览器后退按钮忽略哈希标记? [英] How to make the browser back button disregard hash tags?

查看:127
本文介绍了如何使浏览器后退按钮忽略哈希标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站使用散列来打开/关闭当前页面上的一些标签和图层。使用哈希的原因是,如果用户通过链接访问另一个页面,然后返回到原始页面,则所有标签和图层应该以与离开该页面时相同的方式打开。



问题是,在第一页并使用浏览器后退按钮时,只有哈希值会更改,用户必须多次点击才能返回到最后一个真实页面。






可以改变这种行为吗?以下是一个示例工作流程:

因此,我正在访问一个页面:

start。 php >点击链接> processing.php >点击标签,哈希更改>



processing.php#1 >点击页面上的不同部分,哈希变化为>



processing.php#1-2



现在,当我点击浏览器后退按钮时,我想回到:



start.php NOT processing.php#1






但是!当我离开时:

processing.php#1-2 >导航到> otherpage.php



然后从那里点击后退按钮,我需要回到:

 processing.php#1-2 

为了恢复打开的标签和图层。



这是可能的!?任何帮助表示赞赏!

解决方案

据我所知,如果没有 JavaScript的。这个片段是URL的一部分,当它改变时,新的URL被添加到历史记录中。



我会建议使用 location.replace )当您想要更改网址而不创建历史记录条目时。而不是设置 window.location.hash ,试试这个:

 窗口.location.replace( '#1'); 

我会留给您选择将其整合到您的网站的最佳方法,但这里有一个经过最低限度测试的用于捕捉点击链接的选项:

  $(window).on('click','a' ,函数(e){
var href = $(e.target).attr('href');
if(href& href [0] ==='#'){
window.location.replace(e.target.href);
return false;
}
});


I have a website that uses hashes to open/close some tabs and layers on the current page. The reason behind using hashes is, that if a user visits another page via a link and then goes back to the original page all tabs and layers should open up exactly as they were when leaving that page.

The problem is, that while being on the first page and using the browser back button, only the hashes change and the user must click multiple times to actually go back to the last 'real' page.


Can that behaviour be changed? Here's an example workflow:

So I am visiting a page:

start.php > click link > processing.php > click tab, hash changes >

processing.php#1 > click a different section on the page, hash changes to >

processing.php#1-2

Now when I hit the browser back button I want to go back to:

start.php NOT processing.php#1


BUT! When I go from:

processing.php#1-2 > navigate to > otherpage.php

and then hit the back button from there, I need to go back to:

processing.php#1-2

in order to restore the open tabs and layers.

Is that possible!? Any help is appreciated!

解决方案

As far as I know, you can’t solve this without JavaScript. The fragment is part of the URL, and when it changes, the new URL is added to history.

I would suggest using location.replace() when you want to change the URL without creating a history entry. Instead of setting window.location.hash, try this:

window.location.replace('#1');

I’ll leave it up to you to pick the best way to integrate this into your website, but here’s one minimally-tested option for catching clicks on links:

$(window).on('click', 'a', function(e){
    var href = $(e.target).attr('href');
    if (href && href[0] === '#') {
        window.location.replace(e.target.href);
        return false;
    }
});

这篇关于如何使浏览器后退按钮忽略哈希标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆