最近做公眾號相關, 需要在公眾號里面配菜單, 才發現菜單的鏈接部分是編碼過的, 如這樣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
截圖