window.location.hash 在 Chrome 中刷新?

发布于 2024-11-01 08:08:59 字数 513 浏览 0 评论 0原文

我在网络上进行了一些窥探,发现 window.location.hash = "etc" 是一种广泛采用的方法,可以在不重新加载/刷新页面的情况下更新浏览器的位置。我已将其应用到我编写的这个示例中: http://dl .dropbox.com/u/1595444/locationExample/index.html

在 Safari 中运行良好,但是...

我注意到在 Chrome 10+ 中更改 hash 后:

  • 有类似重新加载的东西。
  • 由此产生的症状是当用户向下或向上滚动时出现打嗝现象。
  • 我的控制台输出被保留(如果您检查控制台,则会输出项目字符串)。
  • 该图标似乎正在重新加载。
  • 以前有人遇到过这个问题吗?知道解决办法吗?

    I was doing some snooping on the web and found window.location.hash = "etc" to be a widely adopted method to update the browser's location without reloading / refreshing the page. I've applied that to this example I've cooked up: http://dl.dropbox.com/u/1595444/locationExample/index.html

    Works well in Safari, but...

    What I've noticed is that in Chrome 10+ upon changing hash:

  • There is something similar to a reload.
  • The resulting symptom is a hiccup as the user scrolls down or up.
  • My console output is preserved (if you check your console the project string's are outputted).
  • The favicon seems to be reloading.
  • Has anyone run into this problem before? Know a fix?

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(3

    窝囊感情。 2024-11-08 08:08:59

    这里很可能发生两件事:

    • 由于 Chrome 错误(即提到 pushState,但哈希更改是
      在相同的代码路径上)。
    • 滚动时出现的轻微问题是因为 Chrome 会进行全页面重绘和高质量缩放来更新页面缩略图,因为它会将哈希更改视为生成新的 URL。这也是一个错误。您可以在检查器时间轴视图中看到这一点,大多数滚动事件都会导致窗口宽度 x 一些小高度的重新绘制,但偶尔会出现全窗口重新绘制。 这篇博文提供了更多详细信息。

    两者的解决方法是推迟哈希值的更新,直到用户完成滚动(您仍然可以立即更新当前项目下出现的白色条)。您可以通过以下方法来做到这一点:

    var scrollTimeout;
    window.onscroll = function() {
      // update current item display here
      if (scrollTimeout)
        clearTimeout(scrollTimeout);
      scrollTimeout = setTimeout(function() {
        scrolTimeout = undefined;
         // update hash here
      }, 100);
    };
    

    因为看起来您正在使用 jQuery,所以有 debounce 插件 这可能会有所帮助。

    There are most likely two things going on here:

    • The favicon and stop/refresh buttons flicker because of a Chrome bug (that mentions pushState, but hash changes are
      on the same code path).
    • The slight hiccup when scrolling is because Chrome does a full page repaint and high-quality scale to update the page thumbnail, since it's considering hash changes as generating a new URL. That's also a bug. You can see this in the inspector timeline view, most scroll events result in a repaint of window width x some small height, but occasionally there will be a full-window repaint. This blog post has a few more details.

    A workaround for both would be to defer the updating of the hash until the user is done scrolling (you can still update the white bar that appears under the current item immediately). You can do this by having something like:

    var scrollTimeout;
    window.onscroll = function() {
      // update current item display here
      if (scrollTimeout)
        clearTimeout(scrollTimeout);
      scrollTimeout = setTimeout(function() {
        scrolTimeout = undefined;
         // update hash here
      }, 100);
    };
    

    Since it looks like you're using jQuery, there are debouncing plugins that may be helpful.

    半窗疏影 2024-11-08 08:08:59

    我没有明确的答案,但首先我会尝试:

    1. 在值前面添加哈希标记 (#)(即使用 window.location.hash = "#etc")。
    2. window.onhashchange 处理程序注册一个处理程序。
    3. 或者,如果您想要完成的任务是,您可以考虑使用 history.pushState使后退按钮返回到之前的逻辑位置(我不清楚您想要完成什么,是否只想跳转到页面上的某个部分,或者更复杂的内容)。

    I don't have a definitive answer, but first I would try:

    1. Prepending the hash mark (#) on to the value (i.e. use window.location.hash = "#etc").
    2. Register a handler for the window.onhashchange handler.
    3. Alternatively, you might consider using history.pushState if what you are trying to accomplish is make the back button return to the previous logical location (it's not clear to me what you are trying accomplish, whether you just want to jump to a section on the page, or something more complex).
    一身仙ぐ女味 2024-11-08 08:08:59
    var r='#hello';
    if(navigator.userAgent.indexOf('Chrome/')!=-1){
     top.history.pushState("", "", r);
     return;
    };
    if(r.charAt(0)=='/'){
      top.location.replace(r);
     }else{
      top.location.hash=r;
    };
    

    为我工作。事实上我花了很长时间才弄清楚这一点。 Firefox 现在还支持 history 对象,因此我们也许可以在几年内摆脱整个“哈希”事物。

    编辑:是的,重新加载是 Chrome 的一个错误。

    var r='#hello';
    if(navigator.userAgent.indexOf('Chrome/')!=-1){
     top.history.pushState("", "", r);
     return;
    };
    if(r.charAt(0)=='/'){
      top.location.replace(r);
     }else{
      top.location.hash=r;
    };
    

    Worked for me. And it actually took me a long time to figure this out. Firefox also supports the history object now, so we may be able to get rid of the whole "hash" thing in a few years.

    EDIT: Yes, the reloading thing is a Chrome bug.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文