天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

Javascript中escape(), encodeURI()和encodeURIComponent()之精析與比較

escape(), encodeURI()和encodeURIComponent()是在Javascript中用于編碼字符串的三個(gè)常用的方法,而他們之間的異同卻困擾了很多的Javascript初學(xué)者,今天我就在這里對(duì)這三個(gè)方法詳細(xì)地分析與比較一下。

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格式返回一個(gè)包含傳入?yún)?shù)內(nèi)容的string類型的值。 Escape方法會(huì)將傳入?yún)?shù)中所有的空格、標(biāo)點(diǎn)符號(hào)、重音字符以及其它任何非ASCII字符替換為%xx的編碼形式,其中xx與其所表示的字符的16進(jìn)制數(shù)表示形式相同。如空格字符的16進(jìn)制表示形式為0x20,則此時(shí)xx應(yīng)為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方法對(duì)于ISO Latin字符集中的字符組成的參數(shù),返回其16進(jìn)制編碼。相對(duì)應(yīng)的,unescape方法則能將16進(jìn)制編碼形式的參數(shù)轉(zhuǎn)化成為其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方法返回一個(gè)經(jīng)過編碼的URI。如果將encodeURI方法的編碼結(jié)果傳遞給decodeURI方法作參數(shù),則能得到原始的未編碼的字符串。需要注意到是encodeURI方法不編碼如下字符":","/",";", and "?"。如果想要編碼這些字符,請(qǐng)使用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.

鄙人譯:通過將每個(gè)屬于特定的字符集合的字符替換為一個(gè)、兩個(gè)或者三個(gè)(為什么是“一個(gè)、兩個(gè)或者三個(gè)”本人也沒有搞懂,望高人賜教)使用UTF-8編碼來表示這個(gè)字符的escape序列來編碼一個(gè)URI。如 ~!@#$%^&*(){}[]=:/,;?+/'"http:// 將被替換為 ~!@#$%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方法返回一個(gè)編碼過的URI。如果將encodeURIComponent方法的編碼結(jié)果傳遞給encodeURIComponent方法作參數(shù),則能得到原始的未編碼的字符串。因?yàn)閑ncodeURIComponent方法會(huì)編碼所有的字符,所以如果待編碼的字符串是用來表示一個(gè)路徑(如/dir1/dir2/index.htm)時(shí),就一定要小心使用了。‘/’符號(hào)會(huì)被其編碼之后,將不再是一個(gè)有效的路徑標(biāo)識(shí)符,所以不能被web服務(wù)器正確地識(shí)別。當(dāng)字符串包含一個(gè)單獨(dú)的URI component(指?后面的請(qǐng)求參數(shù))的時(shí)候,請(qǐng)使用此方法。

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.

鄙人譯:通過將每個(gè)屬于特定的字符集合的字符替換為一個(gè)、兩個(gè)或者三個(gè)(為什么是“一個(gè)、兩個(gè)或者三個(gè)”本人也沒有搞懂,望高人賜教)使用UTF-8編碼來表示這個(gè)字符的escape序列來編碼一個(gè)URIComponent。

有什么區(qū)別?何時(shí)使用?

通過上面的介紹可以看出,MS的文檔明顯要比Mozilla詳細(xì)、易懂一些,但是它們表達(dá)的都是一個(gè)意思。但是escape(), encodeURI()和 encodeURIComponent()有什么異同,它們分別適用于那種特定的情況呢?
escape方法并不編碼字符+。而我們知道,在用戶提交的表單字段中,如果有空格,則會(huì)被轉(zhuǎn)化為+字符,而服務(wù)器解析的時(shí)候則會(huì)認(rèn)為+號(hào)代表空格。由于這個(gè)缺陷,escape方法并不能正確地處理所有的非ASCII字符,你應(yīng)當(dāng)盡量避免使用escape方法,取而代之,你最好選擇encodeURIComponent()方法。
escape()不編碼的字符:@*/+

相對(duì)于使用escape方法,使用encodeURI方法會(huì)顯得更專業(yè)一些。當(dāng)你需要編碼一整個(gè)URI的時(shí)候,你可以使用此方法,因?yàn)閁RI中的合法字符都不會(huì)被編碼轉(zhuǎn)換。需要注意到是字符’也是URI中的合法字符,所以也不會(huì)被編碼轉(zhuǎn)換。
encodeURI()不編碼的字符: ~!@#$&*()=:/,;?+'

encodeURIComponent方法在編碼單個(gè)URIComponent(指請(qǐng)求參數(shù))應(yīng)當(dāng)是最常用的。需要注意到是字符’也是URI中的合法字符,所以也不會(huì)被編碼轉(zhuǎn)換。
encodeURIComponent()不編碼的字符:~!*()'

it知識(shí)庫Javascript中escape(), encodeURI()和encodeURIComponent()之精析與比較,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 日本三区四区免费高清不卡 | 国产午夜免费不卡精品理论片 | 亚洲男女羞羞无遮挡久久丫 | 亚洲性夜色噜噜噜网站2258KK | 伊人狠狠丁香婷婷综合尤物 | 国产高清视频a在线大全 | 国产毛A片久久久久久无码 国产毛A片啊久久久久久A | 好大好硬好湿再深一点网站 | 九九热视频 这里有精品 | 边摸边吃奶边做下面视频 | 日本久久频这里精品99 | 伊人久久大香线蕉影院95 | 国产精品AV色欲蜜臀在线 | 国产精品一区二区免费 | 成人小视频免费在线观看 | 久久精品国产色蜜蜜麻豆国语版 | 国产色播视频在线观看 | 免费看午夜高清性色生活片 | 60老妇性xxxxhd | 高h乱一受多攻男男 | 把腿张开JI巴CAO死你H教室 | 在线 自拍 综合 亚洲 欧美 | 亚洲精品久久久午夜福利电影网 | 后入式啪gif动态图 后入式狂顶免费视频 | 天天夜夜草草久久亚洲香蕉 | 国产成人高清视频 | 白丝制服被啪到喷水很黄很暴力 | 儿媳妇完整版视频播放免费观看 | 久久久无码AV精品亚洲A片软件 | 天天操人人射 | 嘟嘟嘟WWW在线观看视频高清 | 人成片在线观看亚洲无遮拦 | 精品视频在线播放 | 久久久久久免费观看 | 动漫美女和男人下载 | 免费99精品国产自在现线 | 操他射他影院 | 韩国电影real在线观看完整版 | 免费人妻无码AV不卡在线 | 4hu四虎免费影院www | 国产人妻人伦精品59HHH |