on the following URL: www.example.com/page/#2221
- Chrome will return
www.example.com/page/#
fordocument.location.href
- Firefox will return
www.example.com/page/#2221
fordocument.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.
精彩评论