JavaScript encodeURIComponent()

來源:互聯網
上載者:User

標籤:write   uri   log   ascii   pass   編碼   school   document   http   

■ 把字串作為 URI 組件進行編碼。
JavaScript中有三個可以對字串編碼的函數,分別是: escape,encodeURI,encodeURIComponent,相應3個解碼函數:unescape,decodeURI,decodeURIComponent。

● escape()除了 ASCII 字母、數字和特定的符號外,對傳進來的字串全部進行轉義編碼,因此如果想對URL編碼,最好不要使用此方法。

● encodeURI() 用於編碼整個URI,因為URI中的合法字元都不會被編碼轉換。
,/?:@&=+$#。對整個URL進行編碼,而URL的特定標識符不會被轉碼。

● encodeURIComponent方法在編碼單個URIComponent(指請求參數)應當是最常用的,它可以將參數中的中文、特殊字元進行轉義,而不會影響整個URL。
<a href="http://passport.baidu.com/?logout&aid=7&u=‘+encodeURIComponent("http://cang.baidu.com/bruce42")+‘">退出</a>‘);</script>
對URL中的參數進行編碼,因為參數也是一個URL,如果不編碼會影響整個URL的跳轉。
encodeURIComponent() 函數 與 encodeURI() 函數的區別之處。
encodeURIComponent()假定它的參數是 URI 的一部分(比如協議、主機名稱、路徑或查詢字串)。因此 encodeURIComponent() 函數將轉義用於分隔 URI 各個部分的標點符號。

● 傳回值:URIstring 的副本,其中的某些字元將被十六進位的逸出序列進行替換。
該方法不會對 ASCII 字母和數字進行編碼,也不會對這些 ASCII 標點符號進行編碼: - _ . ! ~ * ‘ ( ) 。
其他字元(比如 :;/?:@&=+$,# 這些用於分隔 URI 組件的標點符號),都是由一個或多個十六進位的逸出序列替換的。

● document.write(encodeURIComponent("http://www.w3school.com.cn"))    http%3A%2F%2Fwww.w3school.com.cn
document.write(encodeURIComponent("http://www.w3school.com.cn/p 1/"))    http%3A%2F%2Fwww.w3school.com.cn%2Fp%201%2F
document.write(encodeURIComponent(",/?:@&=+$#"))    %2C%2F%3F%3A%40%26%3D%2B%24%23

JavaScript encodeURIComponent()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.