gpt4 book ai didi

javascript - window.location.href 或 window.location.search

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

我想从 URL 中提取查询字符串值。

因此,在 How can I get query string values in JavaScript? 中投票最多的答案- 使用了 window.location.search

使用window.location.href有什么问题吗?

我试图找出一个问题的根本原因,有时我在使用 location.href 时会得到空的查询字符串值

最佳答案

这 2 个属性返回不同的 things :

href: Is a DOMString containing the whole URL.

和:

search: Is a DOMString containing a '?' followed by the parameters of the URL. Also known as "querystring"

因此您可以使用一个或另一个,只要确保考虑到函数中返回值之间的差异即可。如果您决定使用 href 属性,您需要先提取查询字符串部分(? 之后的部分),然后再将其拆分成多个部分。

关于javascript - window.location.href 或 window.location.search,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34564593/

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