238

How can I escape the # hash sign (sometimes known as number sign or pound sign) sent in the query string of a URL?

0

1 Answer 1

402

Percent encoding. Replace the hash with %23.

4
  • Useful if you want to share a url which contains '#' to twitter Commented Apr 5, 2018 at 2:13
  • 5
    This doesn't work on Chrome 74. Furthermore, encodeURI('#'); is returning # and not the percent encoded character Commented May 20, 2019 at 12:10
  • 28
    # is a valid URI character, but it starts the hash fragment, so you need to encode it in the query string. Compare encodeURIComponent('#'). What do you see in Chrome 74? Commented May 21, 2019 at 14:48
  • 3
    You must also encode a # anywhere in a data: URL, otherwise it will be treated as a fragment identifier.
    – Denis Howe
    Commented Dec 12, 2022 at 19:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.