JS中一些常用函數學習總結

escape()和unescape()

  • escape() 函數可對字符串進行編碼,這樣就可以在所有的計算機上讀取該字符串

語法:
escape(string)
參數描述:
string 必需。要被轉義或編碼的字符串。
返回值:
已編碼的 string 的副本。其中某些字符被替換成了十六進制的轉義序列。
說明:
該方法不會對 ASCII 字母和數字進行編碼,也不會對下面這些 ASCII 標點符號進行編碼:* @ - _ + . / 。其他所有的字符都會被轉義序列替換。
Demo:

<script type="text/javascript">
    var str = "Hello World!^?=()#%&";
    console.log(escape(str));
    //Hello%20World%21%5E%3F%3D%28%29%23%25%26
    console.log(escape("*-_+./@"));//*-_+./@
    </script>
  • unescape()對通過 escape() 編碼的字符串進行解碼

encodeURI()和decodeURI()

  • encodeURI()函數可把字符串作爲 URI 進行編碼

語法:
encodeURI(URIstring)
參數描述:
URIstring 必需。一個字符串,含有URI或其他要編碼的文本。
返回值:
URIstring 的副本,其中的某些字符將被十六進制的轉義序列進行替換。
說明:
該方法不會對 ASCII 字母和數字進行編碼,也不會對這些 ASCII 標點符號進行編碼: - _ . ! ~ * ’ ( ) 。
該方法的目的是:對 URI 進行完整的編碼,因此對以下在 URI 中具有特殊含義的 ASCII 標點符號,encodeURI() 函數是不會進行轉義的:;/?:@&=+$,#
提示:如果 URI 組件中含有分隔符,比如 ? 和 #,則應當使用 encodeURIComponent() 方法分別對各組件進行編碼
Demo:

<script type="text/javascript">
document.write(encodeURI("http://www.baidu.com")+ "<br>");
document.write(encodeURI("http://www.baidu.com/My demo/"));
document.write(encodeURI(",/?:@&=+$#"));
</script>

輸出:

http://www.baidu.com
http://www.baidu.com/My%20demo/
,/?:@&=+$#
  • decodeURI() 函數可對 encodeURI() 函數編碼過的 URI 進行解碼

encodeURIComponent()和decodeURIComponent()

  • encodeURIComponent()函數可把字符串作爲 URI 組件進行編碼

語法:
encodeURIComponent(URIstring)
參數描述:
URIstring 必需。一個字符串,含有 URI 組件或其他要編碼的文本。
返回值:
URIstring 的副本,其中的某些字符將被十六進制的轉義序列進行替換。
說明:
該方法不會對 ASCII 字母和數字進行編碼,也不會對這些 ASCII 標點符號進行編碼: - _ . ! ~ * ’ ( ) 。
其他字符(這也是區別於前兩者的主要所在)(比如 :;/?:@&=+$,# 這些用於分隔 URI 組件的標點符號),都是由一個或多個十六進制的轉義序列替換的。
提示:請注意 encodeURIComponent() 函數 與 encodeURI() 函數的區別之處,前者假定它的參數是 URI 的一部分(比如協議、主機名、路徑或查詢字符串)。因此 encodeURIComponent() 函數將轉義用於分隔 URI 各個部分的標點符號。
Demo:

<script type="text/javascript">
document.write(encodeURIComponent("http://www.baidu.com")+"<br>");
document.write(encodeURIComponent("http://www.baidu.com/my site/")+"<br>");
document.write(encodeURIComponent(",/?:@&=+$#"));
</script>

輸出結果:

http%3A%2F%2Fwww.baidu.com
http%3A%2F%2Fwww.baidu.com%2Fmy%20site%2F
%2C%2F%3F%3A%40%26%3D%2B%24%23
  • decodeURIComponent()函數可對 encodeURIComponent() 函數編碼的 URI 進行解碼
    實例應用:
function addURLParam(url,name,value){
        url += (url.indexOf("?") == -1 ? "?" : "&");
        url += encodeURIComponent(name) + "=" +encodeURIComponent(value);
        return url;
    }
    var url = "http://www.baidu.com";
    //添加參數
    url = addURLParam(url,"name","劉傑");
    url = addURLParam(url,"age",23);
    url = addURLParam(url,"city","北京");
    console.log(url);//http://www.baidu.com?name=%E5%88%98%E6%9D%B0&age=23&city=%E5%8C%97%E4%BA%AC

查詢字符串操作應用:

<script type="text/javascript">
//對查詢字符串進行編碼
function addURLParam(url,name,value){
        url += (url.indexOf("?") == -1 ? "?" : "&");
        url += encodeURIComponent(name) + "=" +encodeURIComponent(value);
        return url;
    }
    var url = "http://www.baidu.com";
    //添加參數
    url = addURLParam(url,"a","1");
    url = addURLParam(url,"b","2");
    url = addURLParam(url,"city","北京");
   console.log(url);//http://www.baidu.com?a=1&b=2&city=%E5%8C%97%E4%BA%AC
   //對查詢字符串進行解碼
   function serilizeUrl(url) {
    var res = {};
    url = url.split("?")[1];
    // console.log(url);//a=1&b=2&city=%E5%8C%97%E4%BA%AC
    var arr = url.split("&");
    // console.log(arr);//["a=1", "b=2", "city=%E5%8C%97%E4%BA%AC"]
    for(var i=0;i<arr.length;i++){
        var temp0 = decodeURIComponent(arr[i].split("=")[0]);
        var temp1 = decodeURIComponent(arr[i].split("=")[1]);
        res[temp0] = temp1;
    }
    return res;
   }
   console.log(serilizeUrl(url));//Object {a: "1", b: "2", city: "北京"}
</script>

總結:
escape()除了 ASCII 字母、數字和特定的符號外,對傳進來的字符串全部進行轉義編碼,因此如果想對URL編碼,最好不要使用此方法(會將URL各部分的分隔符進行編碼,比如://)。而encodeURI() 用於編碼整個URI,因爲URI中的合法字符都不會被編碼轉換。encodeURIComponent方法在編碼單個URIComponent(指請求參數)應當是最常用的,它可以講參數中的中文、特殊字符進行轉義,而不會影響整個URL。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章