Introduction to the use of encodeURI and decodeURI methods in javascript

encodeURI and decodeURI are used in pairs, because if there are Chinese characters in the address bar of the browser, unexpected errors may occur, so encodeURI can convert non-English characters into English encoding, and decodeURI can be used to restore the characters back
1. Basic concepts
encodeURI and decodeURI are used in pairs, because if there are Chinese characters in the address bar of the browser, unexpected errors may occur, so encodeURI can convert non-English characters into English encoding, and decodeURI can be used to encode The characters are restored. The encodeURI method does not encode the following characters: ":", "/", ";" and "?", which the encodeURIComponent method can encode.
The decodeURI() method is equivalent to java.net.URLDecoder.decode(URIString, "UTF-8"); the
encodeURI() method is equivalent to java.net.URLEncoder.encode(URIString, "UTF-8"); 
<script type="text/javascript">
var uriStr = "http://www.baidu.com?name=张三&num=001 zs";
var uriec = encodeURI(uriStr);
document.write("encoded" + uriec);
var uridc = decodeURI (uriec);
document.write("decoded" + uridc);
</script>
  Encoded http://www.baidu.com?name=%E5%BC%A0%E4%B8%89&num=001%20zs 
Decoded http://www.baidu.com?name=Zhang San&num=001 zs 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326783403&siteId=291194637