How can I escape the #
hash sign (sometimes known as number sign or pound sign) sent in the query string of a URL?
Asked
Active
Viewed 1.9e+01k times
231

Peter Mortensen
- 30,738
- 21
- 105
- 131

Muhammad Hewedy
- 29,102
- 44
- 127
- 219
1 Answers
389
Percent encoding. Replace the hash with %23
.

Delgan
- 18,571
- 11
- 90
- 141

Robert Tupelo-Schneck
- 10,047
- 4
- 47
- 58
-
Useful if you want to share a url which contains '#' to twitter – Raynal Gobel Apr 05 '18 at 02:13
-
5This doesn't work on Chrome 74. Furthermore, `encodeURI('#');` is returning `#` and not the percent encoded character – Christian Vincenzo Traina May 20 '19 at 12:10
-
28`#` is a valid URI character, but it starts the [hash fragment](https://en.wikipedia.org/wiki/Fragment_identifier), so you need to encode it in the query string. Compare `encodeURIComponent('#')`. What do you see in Chrome 74? – Robert Tupelo-Schneck May 21 '19 at 14:48
-
You must also encode a `#` anywhere in a `data:` URL, otherwise it will be treated as a fragment identifier. – Denis Howe Dec 12 '22 at 19:20