恐怕它可能是impossible,但是有没有办法更改URL without leaving an entry in the browser's historywithout reloading的散列值呢?还是做同样的事?

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. 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上不起作用.在这种情况下,请使用:

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

除了replaceState()修改当前的历史条目而不是创建新的历史条目之外,history.replaceState()的操作与History y.ush State()完全相同.

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

Jquery相关问答推荐

如果选中了三个sibling ,则选中一个复选框

如何在jQuery中移动表格行?

当输入值以编程方式更改时触发更改事件?

在 textarea 输入中美化 json 数据

jQuery 绑定点击 *ANYTHING* 但 *ELEMENT*

jQuery从字符串中删除字符串

将变量传递给jQuery AJAX成功回调中的函数

是否可以使用 javascript 打开一个弹出窗口,然后检测用户何时关闭它?

如何使用 jQuery 清空输入值?

jQuery .hide() 和 .css("display", "none") 的区别

即使通过Javascript代码判断,如何触发复选框单击事件?

聚焦 时防止 iphone 默认键盘

删除索引后的所有项目

使用 jQuery 拖放防止单击事件

使用 jQuery 按字母顺序对选项元素进行排序

Jquery Ajax 错误处理忽略中止

Javascript 将 Markdown/Textile 转换为 HTML(理想情况下,返回 Markdown/Textile)

PHPStorm IDE 中低效的 jQuery 使用警告

在某个点停止固定位置滚动?

在jQuery中,当它们都具有相同的名称时,如何获取单选按钮的值?