您可以在不影响历史记录的情况下使用哈希导航吗? [英] Can you use hash navigation without affecting history?

查看:137
本文介绍了您可以在不影响历史记录的情况下使用哈希导航吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

恐怕可能是不可能,但是在那里一种方法来更改URL的哈希值而不会在浏览器的历史记录中留下任何条目而无需重新加载?还是等价的?

I'm afraid it might be impossible but is there a way to change the hash value of a URL without leaving an entry in the browser's history and without reloading? Or do the equivalent?

就细节而言,我正在按照以下方式开发一些基本的哈希导航:

As far as specifics go, I was developing some basic hash navigation along the lines of:

//hash nav -- works with js-tabs
var getHash = window.location.hash;
var hashPref = "tab-";
function useHash(newHash) {
    //set js-tab according to hash
    newHash = newHash.replace('#'+hashPref, '');
    $("#tabs li a[href='"+ newHash +"']").click();
}
function setHash(newHash) {
    //set hash according to js-tab
    window.location.hash = hashPref + newHash;

    //THIS IS WHERE I would like to REPLACE the location.hash
    //without a history entry

}
    // ... a lot of irrelavent tabs js and then....

    //make tabs work
    $("#tabs.js-tabs a").live("click", function() {
        var showMe = $(this).attr("href");
        $(showMe).show();
        setHash(showMe);
        return false;
    });
    //hash nav on ready .. if hash exists, execute
    if ( getHash ){
        useHash(getHash);
    }

很显然,使用jQuery.这个想法是,在这种特定情况下:1)让用户返回所有选项卡更改可以通过堆积不必要的引用来有效地破坏后退按钮",以及2)不保留他们所使用的选项卡如果他们刷新了,则当前打开是一件令人烦恼的事情.

Using jQuery, obviously. The idea is that in this specific instance 1) making the user go back over every tab change could effectively 'break the back button' by piling up needless references, and 2) not retaining which tab they're currently on if they hit refresh is an annoyance.

推荐答案

location.replace("#hash_value_here");对我来说效果很好,直到我发现它在IOS Chrome上不起作用.在这种情况下,请使用:

location.replace("#hash_value_here"); worked fine for me until I found that it doesn't work on IOS Chrome. In which case, use:

history.replaceState(undefined, undefined, "#hash_value")

历史记录. replaceState()的操作与history.pushState()完全相同,不同之处在于replaceState()会修改当前的历史记录条目,而不是创建一个新的条目.

history.replaceState() operates exactly like history.pushState() except that replaceState() modifies the current history entry instead of creating a new one.

请记住保留#,否则网址的最后一部分将被更改.

Remember to keep the # or the last part of the url will be altered.

这篇关于您可以在不影响历史记录的情况下使用哈希导航吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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