gpt4 book ai didi

javascript - URL 哈希异常

转载 作者:行者123 更新时间:2023-11-29 22:38:57 25 4
gpt4 key购买 nike

我注意到 JS 中有一些奇怪的行为

window.location.hash = '';
var hash = window.location.hash;
alert(hash + ' = ' + hash.length);
//outputs: ' = 0'
window.location.hash = '#';
hash = window.location.hash;
alert(hash + ' = ' + hash.length);
//outputs: ' = 0'
window.location.hash = '_';
hash = window.location.hash;
alert(hash + ' = ' + hash.length);
//outputs: '_ = 2'

基本上我要触发三个条件

  1. 没有散列
  2. 只是散列
  3. 散列文本

然而,JS 似乎看不出 example.com/和 example.com/之间的区别#我也不知道如何完全删除哈希。

有什么帮助吗?

最佳答案

  1. 一旦设置了散列,您就不能在不导致页面重新加载的情况下将其完全删除(例如,删除 # 符号);这是正常行为。

  2. 设置空/空散列和将散列设置为默认散列 (#) 的处理方式相同;这只是内部行为。不确定是否所有浏览器都一致地处理它,但 IIRC 是这样的。

最终,如果您想完全删除散列,则必须执行 document.location.href = document.location.href,以重新加载页面 (window.location.reload () 将保留哈希)。

关于javascript - URL 哈希异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3888012/

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