1. 程式人生 > >js中decodeURI()和encodeURI()區別,decodeURIComponent和encodeURIComponent區別

js中decodeURI()和encodeURI()區別,decodeURIComponent和encodeURIComponent區別

nbsp sch www 問題 encode 替換 副本 字符替換 序列

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

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

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

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

如下測試代碼:

<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); }

js中decodeURI()和encodeURI()區別,decodeURIComponent和encodeURIComponent區別