简体   繁体   中英

Chrome treats document.location.href differently?

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/#2221 ?

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM