你可以使用哈希导航而不影响历史吗? - Can you use hash navigation without affecting history?

问题:

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 ? 我担心这可能是不可能的,但有没有办法改变URL的哈希值而不在浏览器的历史记录中留下条目 而不重新加载 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);
    }

Using jQuery, obviously. 显然,使用jQuery。 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. 这个想法是在这个具体的例子中 1)让用户回过头来更改每个标签可以通过堆积不必要的引用来有效地“打破后退按钮”,以及2)如果他们点击刷新,则不保留他们当前所在的选项卡是烦恼。


解决方案:

参考: https://stackoom.com/en/question/9feX
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章