Some time ago, I encountered a problem, the back end returned a URL, I need to add a parameter to access directly, but because some of the parameters contain special characters (#), so the browser can not recognize the parameter content after #.

The reason:

The browser treats # as a new parameter because it is a get method. For example: want to request http://xxx/xxxxxx/xx? Id =1000&name= special # character the actual request is http://xxx/xxxxxx/xx? Id = 1000 & special name = #

Solution:

Escape the special character # to an ASCII (hexadecimal) code value.

var name = 'Special # character'
var name2 = ' '
name2 = name.replace(/#/g."% 23"); // Escape # to %23
console.log(name2) // Prints the result: special %23 characters
Copy the code

Simply replace name2 with http://xxx/xxxxxx/xx? Id =1000&name= special %23 characters

Common URL special characters escape

There e are too lazy to write online, cut a map.

Treatment of lazy cancer was abandoned in advanced stage