25

If I console.log(window.location) I get this:

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

I expect search to be ?hello=world, but it isn't.

Why?

I'm using Chrome 35

3
  • 7
    Everything after the # (including the #) is part of window.location.hash. Commented May 21, 2014 at 17:05
  • Best practice is to append the named anchor at the end, see this answer: superuser.com/a/498625/12170 Commented Aug 6, 2020 at 21:27
  • I have the same issue with a React app. My company app has this hash in the url, some strange React format. Commented Jul 18, 2023 at 16:16

1 Answer 1

55

Because ?hello=world isn't considered the query string in your case.
It's part of the anchor (#) which can be obtained using

window.location.hash
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.