" window.location.hash = location.hash"在Webkit(Safari和Chrome)中不起作用 [英] "window.location.hash = location.hash" does not work in Webkit (Safari & Chrome)

查看:128
本文介绍了" window.location.hash = location.hash"在Webkit(Safari和Chrome)中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在Safari中使用 window.location.hash = location.hash

我正在使用JavaScript将我的页面内容用可滚动的DIV进行封装,放置在我的网页导航栏下方。由于滚动条的位置在javascript运行时被重置,因此我将丢失URL设置的原始哈希位置。我需要重新提示散列位置而不使用JavaScript重新加载页面,所以我使用 window.location.hash = location.hash 。它适用于IE8,Firefox和Opera,但在Safari中无法使用。 (我也会假设Chrome,但我没有检查)。任何建议?



提示:我喜欢jQuery。 / div>

Webkit有两个特点,可以防止 window.location.hash = location.hash 正常工作。


  1. Webkit响应 window.location.href 而不是 window.location.hash (与所有其他浏览器一样)。奇怪的是, webkit 仍然可以使用 location.hash hash 标签
  2. Webkit有一个记录错误,其中href 位置必须在浏览器前两次设置到同一位置到新的位置。错误报告此处

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

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

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

}
};


I can't get window.location.hash = location.hash to work in 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 without reloading 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?

Hint: I like jQuery.

解决方案

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

  1. Webkit responds to window.location.href instead of window.location.hash (like all the other browsers do). Curiously, webkit can still read the URL's hash tag using location.hash
  2. Webkit has a documented bug where the href location has to be set to the same location twice before the browser will go to the new location. Bug report here.

This code solved my problem: (using 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;
        }
    }
};

这篇关于" window.location.hash = location.hash"在Webkit(Safari和Chrome)中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆