標籤:
http://www.w3school.com.cn/jsref/jsref_encodeURIComponent.asp
定義和用法
encodeURIComponent() 函數可把字串作為 URI 組件進行編碼。
文法
encodeURIComponent(URIstring)
| 參數 |
描述 |
| URIstring |
必需。一個字串,含有 URI 組件或其他要編碼的文本。 |
傳回值
URIstring 的副本,其中的某些字元將被十六進位的逸出序列進行替換。
說明
該方法不會對 ASCII 字母和數字進行編碼,也不會對這些 ASCII 標點符號進行編碼: - _ . ! ~ * ‘ ( ) 。
其他字元(比如 :;/?:@&=+$,# 這些用於分隔 URI 組件的標點符號),都是由一個或多個十六進位的逸出序列替換的。
提示和注釋
提示:請注意 encodeURIComponent() 函數 與 encodeURI() 函數的區別之處,前者假定它的參數是 URI 的一部分(比如協議、主機名稱、路徑或查詢字串)。因此 encodeURIComponent() 函數將轉義用於分隔 URI 各個部分的標點符號。
encodeURI or encodeURIComponent
It depends on what you are actually wanting to do.
encodeURI assumes that the input is a complete URI that might have some characters which need encoding in it.
encodeURIComponent will encode everything with special meaning, so you use it for components of URIs such as
var world = "A string with symbols / characters that have special meaning?";var uri = ‘http://example.com/foo?hello=‘ + encodeURIComponent(world);
JavaScript encodeURIComponent() 函數