javascript中encodeURI和decodeURI方法
阿新 • • 發佈:2018-11-09
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
一、基本概念encodeURI和decodeURI是成對來使用的,因為瀏覽器的位址列有中文字元的話,可以會出現不可預期的錯誤,所以可以encodeURI把非英文字元轉化為英文編碼,decodeURI可以用來把字元還原回來。encodeURI方法不會對下列字元進行編碼:":"、"/"、";" 和 "?",encodeURIComponent方法可以對這些字元進行編碼。
decodeURI()方法相當於java.net.URLDecoder.decode(URIString, "UTF-8");
encodeURI()方法相當於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("編碼後的" + uriec);
var uridc = decodeURI(uriec);
document.write("解碼後的" + uridc);
</script>
編碼後的http://www.baidu.com?name=%E5%BC%A0%E4%B8%89&num=001%20zs
解碼後的http://www.baidu .com?name=張三&num=001 zs
參考地址:http://blog.csdn.net/hnwangdan/article/details/5968657