为什么window.location.search是空的?

Why is window.location.search empty?

本文关键字:search window location 为什么      更新时间:2023-09-26

如果我console.log(window.location),我得到这个:

Location {replace: function, assign: function, ancestorOrigins: DOMStringList, origin: "https://localhost:3000", hash: "#/account/content?hello=world"…}
ancestorOrigins: DOMStringList
assign: function () { [native code] }
hash: "#/account/content?hello=world"
host: "localhost:3000"
hostname: "localhost"
href: "https://localhost:3000/#/account/content?hello=world'"
origin: "https://localhost:3000"
pathname: "/"
port: "3000"
protocol: "https:"
reload: function reload() { [native code] }
replace: function () { [native code] }
search: ""
toString: function toString() { [native code] }
valueOf: function valueOf() { [native code] }
__proto__: Location

我期望search?hello=world,但它不是。

为什么?

我用的是Chrome 35

因为在您的情况下,?hello=world不被认为是查询字符串。
它是anchor (#)的一部分,可以使用

获取
window.location.hash