Home > Article > Web Front-end > Guide to several ways to encode strings in JS_javascript skills
Function Description
encodeURI() encodes the string into URI
encodeURIComponent() encodes a string into a URI component
escape() encodes a string
The above is to query the data from w3school. So what is the difference between the three? Please allow me to test it.
The printing results are as follows:
As you can see,
encodeURI will not encode the characters used for segmentation in uri such as:/?&;
encodeURIComponent will.
Observing escape, we found that :?& was transcoded, but / was not. The w3school explanation is that the escape function will treat all characters except letters, numbers and symbols (* @ - _ . /) in the ascii code. Encode.
In addition, we can see that the result after escape encodes the Chinese character "China" is different from the first two. W3SCHOOL also recommends not using this method and using the first two instead.
The above is the entire content of this article. I hope it will be helpful to everyone learning javascript.