gpt4 book ai didi

javascript - 删除 URL 哈希字符串中的前几个字符

转载 作者:行者123 更新时间:2023-11-29 17:13:34 25 4
gpt4 key购买 nike

我正在尝试找出如何删除 URL 字符串的特定部分,如:

if (window.location.hash == '#super-super-product') { 
change.window.location.hash.to.this: #product // pseudo code, obviously
}

因此,要删除前 12 个字符的“super-super-”,并保留其余字符,不管它是什么。

以下尝试不会产生任何变化:

if (/^#checkout-counter-./.test(window.location.hash)){ // this works perfectly
window.location.hash.substring(0, 11); // this does nothing
window.location.hash.substr(1, 12); // nothing
window.location.hash.slice(0, 11); // still nothing
}

谢谢。

最佳答案

调用 substring 或任何其他类似方法只会评估函数并返回它而不会产生任何影响。您需要将结果分配给窗口的散列。

window.location.hash = window.location.hash.substring(0, 11);

关于javascript - 删除 URL 哈希字符串中的前几个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19918859/

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