JavaScript decodeURI()與decodeURIComponent() 使用與區別


 

decodeURI()定義和用法:decodeURI()函數可對encodeURI()函數編碼過的URI進行解碼.
語法:decodeURI(URIstring)
參數描述:URIstring必需,一個字符串,含有要解碼的URI組或其他要解碼的文本.
返回值:URIstring的副本,其中的十六進制轉義序列將被它們表示的字符替換.

 

decodeURIComponent()定義和用法:decodeURIComponent()函數可對encodeURIComponent()函數編碼過的URI進行解碼.
語法:decodeURIComponent(URIstring)
參數描述:URIstring必需,一個字符串,含有解碼的URI組件或其他要解碼的文本.
返回值:URIstring的副本,其中的十六進制轉義序列將被它們表示的字符替換.

以上是對於用法的說明,但是在實際的使用過程中有一下問題:

#特殊符號進行進行編碼傳遞參數的時候有一些不一樣:

如下測試代碼:

 

<html>
<head>

<script>

function demo(){

var text=escape("http://www.w3school.com.cn/My first/#qpp");
alert(text);

}
function demo1()
{
var test1="http://www.w3school.com.cn/My first/#qpp"
alert(encodeURIComponent(test1));

}
function demo3()
{
var test1="http://www.w3school.com.cn/My first/#qpp"
alert(decodeURI (test1));

}
function myapp(text)
{
var text=unescape(text);
alert(text);
}
</script>
</head>

<body>
<input type="button" onclick="demo()" value="escape"/><br>
<input type="button" onclick="demo3()" value="decodeURI"/><br>
<input type="button" onclick="demo1()" value="encodeURIComponent"/><br>
<input type="button" onclick="myapp('Visit%20W3School%21')" value="unescape"/><br>
</body>

</html>

 

測試截圖如下:

以上為測試的截圖,對於在查詢字符串中需要# 等一下特殊字符的可以有幫助。


免責聲明!

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



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