位于以下网址:www.example.com/page/#2221
- Chrome 将为
document.location.href
返回www.example.com/page/#
- Firefox 将返回
document.location.href
的www.example.com/page/#2221
还有其他选择可以让它们都返回 www.example.com/page/#2221
吗?
最佳答案
像这样的事情怎么样?
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
。
关于javascript - Chrome 对待 document.location.href 的方式不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5074274/