开发者

Chrome treats document.location.href differently?

开发者 https://www.devze.com 2023-02-12 18:33 出处:网络
on the following URL: www.example.com/page/#2221 Chrome will return www.example.com/page/# for document.location.href

on the following URL: www.example.com/page/#2221

  • Chrome will return www.example.com/page/# for document.location.href
  • Firefox will return www.example.com/page/#2221 for document.location.href

Is there another alternative to have them both return www.example.com/page/#222开发者_Go百科1?


What about something like this?

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.

They return the same thing in Firefox 3.6.13 and Chrome 9.

Though I've always used window.location.


document.location is deprecated. Try using window.location.href instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消