Javascript “window.location.hash = location.hash”在 Webkit(Safari 和 Chrome)中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6970769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:02:41  来源:igfitidea点击:

"window.location.hash = location.hash" does not work in Webkit (Safari & Chrome)

javascriptgoogle-chromesafariwebkit

提问by Brandon Lebedev

I can't get window.location.hash = location.hashto work in Safari.

我无法window.location.hash = location.hash在 Safari 中工作。

I'm using javascript to wrap the contents of my page with a scrollable DIV, placed below the navigation bar in my webpage. Since the scrollbar's location gets reset when the javascript runs, I'm losing the original hash location that the URL set. I need to re-cue the hash location withoutreloading the page using javascript, so I'm using window.location.hash = location.hash. It works in IE8, Firefox, and Opera, but it doesn't work in Safari. (I'll assume Chrome, too, but I haven't check). Any suggestions?

我正在使用 javascript 用可滚动的 DIV 包装我的页面内容,该 DIV 位于我网页中的导航栏下方。由于滚动条的位置在 javascript 运行时被重置,我丢失了 URL 设置的原始哈希位置。我需要在使用 javascript 重新加载页面的情况下重新提示哈希位置,所以我使用window.location.hash = location.hash. 它适用于 IE8、Firefox 和 Opera,但不适用于 Safari。(我也会假设 Chrome,但我还没有检查)。有什么建议?

Hint: I like jQuery.

提示:我喜欢 jQuery。

回答by Brandon Lebedev

Webkit has two oddities that prevent window.location.hash = location.hashfrom working normally.

Webkit 有两个奇怪的地方,window.location.hash = location.hash无法正常工作。

  1. Webkit responds to window.location.hrefinstead of window.location.hash(like all the other browsers do). Curiously, webkitcan still read the URL's hashtag using location.hash
  2. Webkit has a documented bug where the href locationhas to be set to the same location twice before the browser will go to the new location. Bug report here.
  1. Webkit 响应window.location.href而不是window.location.hash(就像所有其他浏览器一样)。奇怪的是,webkit仍然可以hash使用读取 URL 的标签location.hash
  2. Webkit 有一个记录的错误,即location在浏览器转到新位置之前,必须将href设置为同一位置两次。错误报告在这里

This code solved my problem: (using jQuery).

这段代码解决了我的问题:(使用 jQuery)。

$(document).ready(function() {
    gotoHASH()
};

function gotoHASH() {
    if (location.hash) {
        if ( $.browser.webkit == false ) {
            window.location.hash = location.hash;
        } else {
            window.location.href = location.hash;
        }
    }
};

回答by Michael Baldry

I ended up with

我结束了

window.location.hash = "";
window.location.hash = "myanchor";

This worked fine in all desktop browsers I tested in and on iOS and Android chrome.

这在我在 iOS 和 Android chrome 中测试过的所有桌面浏览器中都运行良好。

回答by evan

Set location.hash to something else first and immediately set it back.

首先将 location.hash 设置为其他内容,然后立即将其设置回去。

var t = window.location.hash;
window.location.hash = "non-existant-id";
window.location.hash = t;

回答by sv_in

Before JavaScript changes the orginal hash location, get the scroll position using

在 JavaScript 更改原始哈希位置之前,使用

var st = $(window).scrollTop().

When you want to restore the scroll location, use

当你想恢复滚动位置时,使用

$(window).scrollTop(st);

回答by decoder7283

go_hash('#home')

The function...

功能...

function go_hash(hash) {
  console.log('go_hash: ' + hash)
  if(hash.indexOf('#') == -1)
    hash = '#' + hash
  if(document.location.hash) {
    document.location.hash = hash
    return
  }
  if(window.location.hash) {
    window.location.hash = hash
    return
  }
  if(document.location.href) {
    document.location.href = hash
    return
  }
  window.location.href = hash
}