gpt4 book ai didi

jquery - 空哈希和无哈希的区别

转载 作者:行者123 更新时间:2023-12-03 23:01:32 25 4
gpt4 key购买 nike

使用 jQuery,有没有办法区分当前 window.location 上没有哈希值和空哈希值?

这就是我所说的“空哈希”:

http://domain.tld/#

这是“无哈希”:

http://domain.tld/

最佳答案

对于无哈希和空哈希,

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
}

或者根据您的请求首先检查现有哈希:

if (window.location.hash) {
// Non-empty hash
}
else if (window.location.href.split("#").length == 1) {
// No hash
}
else {
// Empty hash
}

另请参阅:How to remove the hash from window.location with JavaScript without page refresh?

关于jquery - 空哈希和无哈希的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15113752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com