Chrome对document.location.href的处理方式有何不同?

时间:2011-02-22 05:11:27

标签: javascript firefox google-chrome

位于以下网址:www.example.com/page/#2221

  • Chrome将为www.example.com/page/#
  • 返回document.location.href
  • Firefox会为www.example.com/page/#2221
  • 返回document.location.href

还有另一种选择让他们都返回www.example.com/page/#2221吗?

2 个答案:

答案 0 :(得分:1)

document.location已弃用。请尝试使用window.location.href

答案 1 :(得分:1)

这样的事情怎么样?

function getUrl() {
  var location = document.location.href,
      locationLength = location.length,
      hash = document.location.hash;

    if (hash 
        && location.substring(locationLength - 1, locationLength) == '#') {
        location += hash;
    }

    return location;
}

jsFiddle

他们在Firefox 3.6.13和Chrome 9中返回相同的内容。

虽然我一直使用window.location