js對文字進行編碼涉及3個函數:escape,encodeURI,encodeURIComponent,相應3個解碼函數:unescape,decodeURI,decodeURIComponentjavascript
一、 傳遞參數時須要使用encodeURIComponent,這樣組合的url纔不會被#等特殊字符截斷。 html
例如:<script language="javascript">document.write('<a href="http://passport.baidu.com/?logout&aid=7& u='+encodeURIComponent("http://cang.baidu.com/bruce42")+'">退出</a& amp; gt;');</script>java
二、 進行url跳轉時能夠總體使用encodeURIweb
例如:Location.href=encodeURI("http://cang.baidu.com/do/s?word=百度& ct=21");服務器
三、 js使用數據時可使用escapeide
[Huoho.Com編輯]函數
例如:蒐藏中history紀錄。ui
四、 escape對0-255之外的unicode值進行編碼時輸出%u****格式,其它狀況下 escape,encodeURI,encodeURIComponent編碼結果相同。編碼
最多使用的應爲encodeURIComponent,它是將中文、韓文等特殊字符轉換成utf-8格式的url編碼,因此若是給後臺傳遞參數須要使用encodeURIComponent時須要後臺解碼對utf-8支持(form中的編碼方式和當前頁面編碼方式相同)url
escape不編碼字符有69個:*,+,-,.,/,@,_,0-9,a-z,A-Z
encodeURI不編碼字符有82個:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a- z,A-Z
encodeURIComponent不編碼字符有71個:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z
javaScript中URL編碼轉換,escape() encodeURI() encodeURIComponent
2007年05月12日 星期六 下午 04:48
在使用url進行參數傳遞時,常常會傳遞一些中文名的參數或URL地址,在後臺處理時會發生轉換錯誤。在有些傳遞頁面使用GB2312,而在接收 頁面使用UTF8,這樣接收到的參數就可能會與原來發生不一致。使用服務器端的urlEncode函數編碼的URL,與使用客戶端javascript的 encodeURI函數編碼的URL,結果就不同。
javaScript中的編碼方法:
escape() 方法:
採用ISO Latin字符集對指定的字符串進行編碼。全部的空格符、標點符號、特殊字符以及其餘非ASCII字符都將被轉化成%xx格式的字符編碼(xx等於該字符 在字符集表裏面的編碼的16進制數字)。好比,空格符對應的編碼是%20。unescape方法與此相反。不會被此方法編碼的字符: @ * / +
encodeURI() 方法:把URI字符串採用UTF-8編碼格式轉化成escape格式的字符串。不會被此方法編碼的字符:! @ # $& * ( ) = : / ; ? + '
encodeURIComponent() 方法:把URI字符串採用UTF-8編碼格式轉化成escape格式的字符串。與encodeURI()相比,這個方法將對更多的字符進行編碼,好比 / 等字符。因此若是字符串裏面包含了URI的幾個部分的話,不能用這個方法來進行編碼,不然 / 字符被編碼以後URL將顯示錯誤。不會被此方法編碼的字符:! * ( )
所以,對於中文字符串來講,若是不但願把字符串編碼格式轉化成UTF-8格式的(好比原頁面和目標頁面的charset是一致的時候),只須要 使用escape。若是你的頁面是GB2312或者其餘的編碼,而接受參數的頁面是UTF-8編碼的,就要採用 encodeURI或者encodeURIComponent。
另外,encodeURI/encodeURIComponent是在javascript1.5以後引進的,escape則在 javascript1.0版本就有。
escape() 方法
MSDN JScript Reference中如是說:
The escape method returns a string value (in Unicode format) that contains the contents of [the argument]. All spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding, where xx is equivalent to the hexadecimal number representing the character. For example, a space is returned as "%20."
譯:escape方法以Unicode格式返回一個包含傳入參數內容的string類型的值。 Escape方法會將傳入參數中全部的空格、標點符號、重音字符以及其它任何非ASCII字符替換爲%xx的編碼形式,其中xx與其所表示的字符的16進 制數表示形式相同。如空格字符的16進製表示形式爲0x20,則此時xx應爲20,即escape(‘ ’) 返回「%20」。
Mozilla Developer Core Javascript Guide中如是說:
The escape and unescape functions let you encode and decode strings. The escape function returns the hexadecimal encoding of an argument in the ISO Latin character set. The unescape function returns the ASCII string for the specified hexadecimal encoding value.
譯:escape和unescape方法可以幫助你編碼和解碼字符串。escape方法對於ISO Latin字符集中的字符組成的參數,返回其16進制編碼。相對應的,unescape方法則能將16進制編碼形式的參數轉化成爲其ASCII碼形式。
encodeURI()方法
MSDN JScript Reference中如是說:
The encodeURI method returns an encoded URI. If you pass the result to decodeURI, the original string is returned. The encodeURI method does not encode the following characters: ":", "/", ";", and "?". Use encodeURIComponent to encode these characters.
譯:encodeURI方法返回一個通過編碼的URI。若是將encodeURI方法的編碼結果傳遞給decodeURI方法做參數,則能獲得原始 的未編碼的字符串。須要注意到是encodeURI方法不編碼以下字符":", "/", ";", and "?"。若是想要編碼這些字符,請使用encodeURIComponent方法。
Mozilla Developer Core Javascript Guide中如是說:
Encodes a Uniform Resource Identifier (URI) by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character.
譯:經過將每一個屬於特定的字符集合的字符替換爲一個、兩個或者三個(爲何是「一個、兩個或者三個」本人也沒有搞懂,望高人賜教)使用UTF-8編碼來表示這個字符的escape序列來編碼一個URI。如 ~!@#$%^&*(){}[]=:/,;?+\''"\\ 將被替換爲 ~!@#$%25%5E&*()%7B%7D%5B%5D=:/,;?+''%22%5C
encodeURIComponent()方法
MSDN JScript Reference中如是說:
The encodeURIComponent method returns an encoded URI. If you pass the result to decodeURIComponent, the original string is returned. Because the encodeURIComponent method encodes all characters, be careful if the string represents a path such as /folder1/folder2/default.html. The slash characters will be encoded and will not be valid if sent as a request to a web server. Use the encodeURI method if the string contains more than a single URI component.
譯:encodeURIComponent方法返回一個編碼過的URI。若是將encodeURIComponent方法的編碼結果傳遞給 encodeURIComponent方法做參數,則能獲得原始的未編碼的字符串。由於encodeURIComponent方法會編碼全部的字符,因此 若是待編碼的字符串是用來表示一個路徑(如/dir1/dir2/index.htm)時,就必定要當心使用了。‘/’符號會被其編碼以後,將再也不是一個 有效的路徑標識符,因此不能被web服務器正確地識別。當字符串包含一個單獨的URI component(指?後面的請求參數)的時候,請使用此方法。
Mozilla Developer Core Javascript Guide中如是說:
Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character.
譯:經過將每一個屬於特定的字符集合的字符替換爲一個、兩個或者三個(爲何是「一個、兩個或者三個」本人也沒有搞懂,望高人賜教)使用UTF-8編碼來表示這個字符的escape序列來編碼一個URIComponent。
有什麼區別?什麼時候使用?
經過上面的介紹能夠看出,MS的文檔明顯要比Mozilla詳細、易懂一些,可是它們表達的都是一個意思。可是escape(), encodeURI()和 encodeURIComponent()有什麼異同,它們分別適用於那種特定的狀況呢?
escape 方法並不編碼字符+。而咱們知道,在用戶提交的表單字段中,若是有空格,則會被轉化爲+字符,而服務器解析的時候則會認爲+號表明空格。因爲這個缺 陷,escape方法並不能正確地處理全部的非ASCII字符,你應當儘可能避免使用escape方法,取而代之,你最好選擇 encodeURIComponent()方法。
escape()不編碼的字符:@*/+
相對於使用escape方法,使用encodeURI方法會顯得更專業一些。當你須要編碼一整個URI的時候,你可使用此方法,由於URI中的合法字符都不會被編碼轉換。須要注意到是字符’也是URI中的合法字符,因此也不會被編碼轉換。
encodeURI() 不編碼的字符: ~!@#$&*()=:/,;?+''
encodeURIComponent方法在編碼單個URIComponent(指請求參數)應當是最經常使用的。須要注意到是字符’也是URI中的合法字符,因此也不會被編碼轉換。
encodeURIComponent()不編碼的字符: ~!*()''