使用jQuery,有没有办法区分当前没有哈希和空哈希window.location?
这就是我所说的"空哈希":
http://domain.tld/#
Run Code Online (Sandbox Code Playgroud)
这是"没有哈希":
http://domain.tld/
Run Code Online (Sandbox Code Playgroud)
window.location.hash将返回""无哈希和空哈希.如果你需要做某种原因的区别,你可能分裂window.location.href的#:
var frag = window.location.href.split("#");
if (frag.length == 1) {
// No hash
}
else if (!frag[1].length) {
// Empty hash
}
else {
// Non-empty hash
}
Run Code Online (Sandbox Code Playgroud)
或者根据您的要求首先检查现有哈希值:
if (window.location.hash) {
// Non-empty hash
}
else if (window.location.href.split("#").length == 1) {
// No hash
}
else {
// Empty hash
}
Run Code Online (Sandbox Code Playgroud)
另请参阅:如何在没有页面刷新的情况下使用JavaScript从window.location中删除哈希?