js對url的編碼和解碼


 

最近做公眾號相關, 需要在公眾號里面配菜單, 才發現菜單的鏈接部分是編碼過的, 如這樣http%3A%2F%2Fw3cschool.cn%2Fmy%20test.asp%3Fname%3Dst%C3%A5le%26car%3Dsaab

當時不太明白, 后來在網上看了看, 搜了搜, 發現js對url編碼就是這樣, 所以在這里簡單總結一下

使用 encodeURIComponent() 方法可以對 URI 進行編碼。

使用 decodeURIComponent() 方法可以對 URI 進行解碼.

w3c提供了簡單實現

<script>

var uri="http://w3cschool.cn/my test.php?name=ståle&car=saab";
var uri_encode=encodeURIComponent(uri);
document.write(uri_encode);
document.write("<br>");
document.write(decodeURIComponent(uri_encode));

</script>

結果

http%3A%2F%2Fw3cschool.cc%2Fmy%20test.php%3Fname%3Dst%C3%A5le%26car%3Dsaab
http://w3cschools.com/my test.asp?name=ståle&car=saab

 

截圖

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM