手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >URL编码转换,escape() encodeURI() encodeURIComponent()
URL编码转换,escape() encodeURI() encodeURIComponent()
摘要:escape()方法:采用ISOLatin字符集对指定的字符串进行编码。所有的空格符、标点符号、特殊字符以及其他非ASCII字符都将被转化成...

escape()方法:

采用ISOLatin字符集对指定的字符串进行编码。所有的空格符、标点符号、特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符在字符集表里面的编码的16进制数字)。比如,空格符对应的编码是%20。unescape方法与此相反。不会被此方法编码的字符:@*/+

英文解释:MSDNJScriptReference:Theescapemethodreturnsastringvalue(inUnicodeformat)thatcontainsthecontentsof[theargument].Allspaces,punctuation,accentedcharacters,andanyothernon-ASCIIcharactersarereplacedwith%xxencoding,wherexxisequivalenttothehexadecimalnumberrepresentingthecharacter.Forexample,aspaceisreturnedas"%20."

EdgeCoreJavascriptGuide:Theescapeandunescapefunctionsletyouencodeanddecodestrings.TheescapefunctionreturnsthehexadecimalencodingofanargumentintheISOLatincharacterset.TheunescapefunctionreturnstheASCIIstringforthespecifiedhexadecimalencodingvalue.

encodeURI()方法:把URI字符串采用UTF-8编码格式转化成escape格式的字符串。不会被此方法编码的字符:!@#$&*()=:/;?+'

英文解释:MSDNJScriptReference:TheencodeURImethodreturnsanencodedURI.IfyoupasstheresulttodecodeURI,theoriginalstringisreturned.TheencodeURImethoddoesnotencodethefollowingcharacters:":","/",";",and"?".UseencodeURIComponenttoencodethesecharacters.EdgeCoreJavascriptGuide:EncodesaUniformResourceIdentifier(URI)byreplacingeachinstanceofcertaincharactersbyone,two,orthreeescapesequencesrepresentingtheUTF-8encodingofthecharacter

encodeURIComponent()方法:把URI字符串采用UTF-8编码格式转化成escape格式的字符串。与encodeURI()相比,这个方法将对更多的字符进行编码,比如/等字符。所以如果字符串里面包含了URI的几个部分的话,不能用这个方法来进行编码,否则/字符被编码之后URL将显示错误。不会被此方法编码的字符:!*()

英文解释:MSDNJScriptReference:TheencodeURIComponentmethodreturnsanencodedURI.IfyoupasstheresulttodecodeURIComponent,theoriginalstringisreturned.BecausetheencodeURIComponentmethodencodesallcharacters,becarefulifthestringrepresentsapathsuchas/folder1/folder2/default.html.Theslashcharacterswillbeencodedandwillnotbevalidifsentasarequesttoawebserver.UsetheencodeURImethodifthestringcontainsmorethanasingleURIcomponent.MozillaDeveloperCoreJavascriptGuide:EncodesaUniformResourceIdentifier(URI)componentbyreplacingeachinstanceofcertaincharactersbyone,two,orthreeescapesequencesrepresentingtheUTF-8encodingofthecharacter.

复制代码 代码如下:因此,对于中文字符串来说,如果不希望把字符串编码格式转化成UTF-8格式的(比如原页面和目标页面的charset是一致的时候),只需要使用escape。如果你的页面是GB2312或者其他的编码,而接受参数的页面是UTF-8编码的,就要采用encodeURI或者encodeURIComponent。另外,encodeURI/encodeURIComponent是在javascript1.5之后引进的,escape则在javascript1.0版本就有。

英文注释:Theescape()methoddoesnotencodethe+characterwhichisinterpretedasaspaceontheserversideaswellasgeneratedbyformswithspacesintheirfields.Duetothisshortcoming,youshouldavoiduseofescape()wheneverpossible.ThebestalternativeisusuallyencodeURIComponent().UseoftheencodeURI()methodisabitmorespecializedthanescape()inthatitencodesforURIs[REF]asopposedtothequerystring,whichispartofaURL.UsethismethodwhenyouneedtoencodeastringtobeusedforanyresourcethatusesURIsandneedscertaincharacterstoremainun-encoded.Notethatthismethoddoesnotencodethe'character,asitisavalidcharacterwithinURIs.Lastly,theencodeURIComponent()methodshouldbeusedinmostcaseswhenencodingasinglecomponentofaURI.ThismethodwillencodecertaincharsthatwouldnormallyberecognizedasspecialcharsforURIssothatmanycomponentsmaybeincluded.Notethatthismethoddoesnotencodethe'character,asitisavalidcharacterwithinURIs.

【URL编码转换,escape() encodeURI() encodeURIComponent()】相关文章:

详解Javascript中的Object对象

Javascript - HTML的request类

JavaScript正则表达式的分组匹配详解

美化下拉列表

理解javascript中的with关键字

javascript小技巧 超强推荐第1/5页

nodejs调试cmd命令实现复制目录

JavaScript获取并更改input标签name属性的方法

Javascript技术栈中的四种依赖注入小结

原生js实现的贪吃蛇网页版游戏完整实例

精品推荐
分类导航