使用JAVASCRIPT編碼URL注意事項

http://zh.wikipedia.org/wiki/URI

,統一資源標識符(Uniform Resource Identifier,URI)一文中,URI可被視為定位符(URL),名稱(URN)或兩者兼備。而URLs 只能使用 ASCII character-set.字集在網路中傳遞。所以常常需要對URL中的許多字元執行重新編碼與解碼的動作。

 

依據RFC1630RFC1738RFC2141RFC2732RFC2396RFC3986URL中有下列保留字

()‘!,[]+$= ;/#?:@&% 空白

 

若有下列字元,在某些格式下,必須經過編碼處理,以免發生錯誤

# % { } | \ ^ ~ [ ] `<>空白 

 

1.URL中若有上列字元一定需要編碼

2.encodeURI()decodeURIComponent()會對URL中有特殊意義的字作編碼動作,以UTF-8格式編碼。

3.escape()會將特定的字元執行編碼動作,以UTF-16格式編碼。

4.使用Javascript透過JSON傳遞資料時可使用escape()編碼資料、unescape()解碼資料。

5. encodeURI()用來編碼整個網址,對URL中有特殊意義的字作編碼動作。

6. encodeURIComponent()主要是針對URL後面傳遞的參數作編碼動作,對URL中有特殊意義的字作編碼動作。

7.若是需要透過JAVASCRIPT執行網址跳轉或是使用Get方法傳遞參數,請使用

encodeURI()編碼網址、decodeURI()解碼網址、encodeURIComponent()編碼網址後面的參數資料、decodeURIComponent()解碼網址後面的參數資料,建議使用encodeURIComponent()URL編碼動作

8.使用上列編碼函數時,請先確認網頁<meta charset="UTF-8" />檔案是否以UTF-8格式儲存,還有Web Server處理網址的格式,若不是,則須做其他處理。

 

編碼測試:

小寫英文字母編碼/解碼測試lowerCaseLetters = abcdefghijklmnopqrstuvwxyz
escape(lowerCaseLetters) ==> abcdefghijklmnopqrstuvwxyz
unescape(lowerCaseLetters) ==> abcdefghijklmnopqrstuvwxyz
encodeURI(lowerCaseLetters) ==> abcdefghijklmnopqrstuvwxyz
decodeURI(lowerCaseLetters) ==> abcdefghijklmnopqrstuvwxyz
encodeURIComponent(lowerCaseLetters) ==> abcdefghijklmnopqrstuvwxyz
decodeURIComponent(lowerCaseLetters) ==> abcdefghijklmnopqrstuvwxyz
大寫英文字母編碼/解碼測試uppercaseLetters = ABCDEFGHIJKLMNOPQRSTUVWXYZ
escape(uppercaseLetters) ==> ABCDEFGHIJKLMNOPQRSTUVWXYZ
unescape(uppercaseLetters) ==> ABCDEFGHIJKLMNOPQRSTUVWXYZ
encodeURI(uppercaseLetters) ==> ABCDEFGHIJKLMNOPQRSTUVWXYZ
decodeURI(uppercaseLetters) ==> ABCDEFGHIJKLMNOPQRSTUVWXYZ
encodeURIComponent(uppercaseLetters) ==> ABCDEFGHIJKLMNOPQRSTUVWXYZ
decodeURIComponent(uppercaseLetters) ==> ABCDEFGHIJKLMNOPQRSTUVWXYZ
數字編碼/解碼測試numberStr = 0123456789
escape(numberStr) ==> 0123456789
unescape(numberStr) ==> 0123456789
encodeURI(numberStr) ==> 0123456789
decodeURI(numberStr) ==> 0123456789
encodeURIComponent(numberStr) ==> 0123456789
decodeURIComponent(numberStr) ==> 0123456789
escape()不執行編碼的字元編碼/解碼測試escapeUnEncodeStr = *@-_+./
escape(escapeUnEncodeStr) ==> *@-_+./
unescape(escapeUnEncodeStr) ==> *@-_+./
encodeURI()不執行編碼的字元編碼/解碼測試encodeURIUnEncodeStr = ;'()!~#$=&:?,*@-_+./
encodeURI(encodeURIUnEncodeStr) ==> ;'()!~#$=&:?,*@-_+./
decodeURI(encodeURIUnEncodeStr) ==> ;'()!~#$=&:?,*@-_+./
encodeURIComponent()不執行編碼的字元編碼/解碼測試encodeURIComponentUnEncodeStr = !'()*-._~
encodeURIComponent(encodeURIComponentUnEncodeStr) ==> !'()*-._~
decodeURIComponent(encodeURIComponentUnEncodeStr) ==> !'()*-._~
空白編碼/解碼測試encodeSpace =
escape(encodeSpace) ==> %20
unescape(encodeSpace) ==>
encodeURI(encodeSpace) ==> %20
decodeURI(encodeSpace) ==>
encodeURIComponent(encodeSpace) ==> %20
decodeURIComponent(encodeSpace) ==>
難字編碼/解碼測試specialChineseWord = 許功蓋育堃煊栢喆綫綉滙峯頴邨着双
escape(specialChineseWord) ==> %u8A31%u529F%u84CB%u80B2%u5803%u714A%u6822%u5586%u7DAB%u7D89%u6ED9%u5CEF%u9834%u90A8%u7740%u53CC
unescape(specialChineseWord) ==> 許功蓋育堃煊栢喆綫綉滙峯頴邨着双
encodeURI(specialChineseWord) ==> %E8%A8%B1%E5%8A%9F%E8%93%8B%E8%82%B2%E5%A0%83%E7%85%8A%E6%A0%A2%E5%96%86%E7%B6%AB%E7%B6%89%E6%BB%99%E5%B3%AF%E9%A0%B4%E9%82%A8%E7%9D%80%E5%8F%8C
decodeURI(specialChineseWord) ==> 許功蓋育堃煊栢喆綫綉滙峯頴邨着双
encodeURIComponent(specialChineseWord) ==> %E8%A8%B1%E5%8A%9F%E8%93%8B%E8%82%B2%E5%A0%83%E7%85%8A%E6%A0%A2%E5%96%86%E7%B6%AB%E7%B6%89%E6%BB%99%E5%B3%AF%E9%A0%B4%E9%82%A8%E7%9D%80%E5%8F%8C
decodeURIComponent(specialChineseWord) ==> 許功蓋育堃煊栢喆綫綉滙峯頴邨着双

 

經上列測試,可得到:

escape()不編碼A-Z、a-z、0-9、*@-_+./

encodeURI ()不編碼A-Z、a-z、0-9、;'()!~#$=&:?,*@-_+./

encodeURIComponent ()不編碼A-Z、a-z、0-9、!'()*-._~

 

參考資料 

JavaScript encodeURI() Function

http://www.w3schools.com/jsref/jsref_encodeuri.asp

 

JavaScript encodeURIComponent() Function

http://www.w3schools.com/jsref/jsref_encodeuricomponent.asp

 

JavaScript escape() Function

http://www.w3schools.com/jsref/jsref_escape.asp 

http://www.w3schools.com/tags/ref_urlencode.asp

http://www.w3schools.com/tags/ref_ascii.asp

 

http://www.blooberry.com/indexdot/html/topics/urlencoding.htm

http://www.cnblogs.com/s1ihome/archive/2008/05/06/1184254.html

http://samwang823.blogspot.tw/2010/06/javascript-urlencode-escape-encodeuri.html

http://www.javascripter.net/faq/escape.htm

http://www.animalgenome.org/community/angenmap/URLEncoding.html

http://javascript.about.com/library/blencode.htm

 

, , , , , , , ,

K 發表在 痞客邦 PIXNET 留言(0) 人氣()