你可以使用哈希導航而不影響歷史嗎? - 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章