`

JAVASCRIPT基础学习篇(10)附2-URL编码转换,escape() encodeURI() encodeURIComponent()

阅读更多
  1. escape()方法:
  2. 采用ISOLatin字符集对指定的字符串进行编码。所有的空格符、标点符号、特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符在字符集表里面的编码的16进制数字)。比如,空格符对应的编码是%20。unescape方法与此相反。不会被此方法编码的字符:@*/+
  3. 英文解释:MSDNJScriptReference:Theescapemethodreturnsastringvalue(inUnicodeformat)thatcontainsthecontentsof[theargument].Allspaces,punctuation,accentedcharacters,andanyothernon-ASCIIcharactersarereplacedwith%xxencoding,wherexxisequivalenttothehexadecimalnumberrepresentingthecharacter.Forexample,aspaceisreturnedas"%20."
  4. EdgeCoreJavascriptGuide:Theescapeandunescapefunctionsletyouencodeanddecodestrings.TheescapefunctionreturnsthehexadecimalencodingofanargumentintheISOLatincharacterset.TheunescapefunctionreturnstheASCIIstringforthespecifiedhexadecimalencodingvalue.
  5. encodeURI()方法:把URI字符串采用UTF-8编码格式转化成escape格式的字符串。不会被此方法编码的字符:!@#$&*()=:/;?+'
  6. 英文解释:MSDNJScriptReference:TheencodeURImethodreturnsanencodedURI.IfyoupasstheresulttodecodeURI,theoriginalstringisreturned.TheencodeURImethoddoesnotencodethefollowingcharacters:":","/",";",and"?".UseencodeURIComponenttoencodethesecharacters.EdgeCoreJavascriptGuide:EncodesaUniformResourceIdentifier(URI)byreplacingeachinstanceofcertaincharactersbyone,two,orthreeescapesequencesrepresentingtheUTF-8encodingofthecharacter
  7. encodeURIComponent()方法:把URI字符串采用UTF-8编码格式转化成escape格式的字符串。与encodeURI()相比,这个方法将对更多的字符进行编码,比如/等字符。所以如果字符串里面包含了URI的几个部分的话,不能用这个方法来进行编码,否则/字符被编码之后URL将显示错误。不会被此方法编码的字符:!*()
  8. 英文解释:MSDNJScriptReference:TheencodeURIComponentmethodreturnsanencodedURI.IfyoupasstheresulttodecodeURIComponent,theoriginalstringisreturned.BecausetheencodeURIComponentmethodencodesallcharacters,becarefulifthestringrepresentsapathsuchas/folder1/folder2/default.html.Theslashcharacterswillbeencodedandwillnotbevalidifsentasarequesttoawebserver.UsetheencodeURImethodifthestringcontainsmorethanasingleURIcomponent.MozillaDeveloperCoreJavascriptGuide:EncodesaUniformResourceIdentifier(URI)componentbyreplacingeachinstanceofcertaincharactersbyone,two,orthreeescapesequencesrepresentingtheUTF-8encodingofthecharacter.
  9. 引用内容
  10. 因此,对于中文字符串来说,如果不希望把字符串编码格式转化成UTF-8格式的(比如原页面和目标页面的charset是一致的时候),只需要使用escape。如果你的页面是GB2312或者其他的编码,而接受参数的页面是UTF-8编码的,就要采用encodeURI或者encodeURIComponent。
  11. 另外,encodeURI/encodeURIComponent是在javascript1.5之后引进的,escape则在javascript1.0版本就有。
  12. 英文注释: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.
分享到:
评论

相关推荐

    JS中三种编码方式(escape,encodeURI,encodeURIComponent)

    本文将详细探讨三种主要的编码方法:`escape()`, `encodeURI()`, 和 `encodeURIComponent()`,并解释它们之间的区别和应用场景。 首先,`escape()` 方法并不常用于现代Web开发,但它仍然是JavaScript内置的一个函数...

    escape、encodeURI、encodeURIComponent 区别详解

    ### escape、encodeURI、encodeURIComponent 区别详解 在前端开发中,经常需要用到字符串编码与解码的方法来确保数据在网络传输中的正确性与安全性。本文将详细介绍 `escape()`、`encodeURI()` 和 `...

    url的三个js编码函数escape(),encodeURI(),encodeURIComponent()简介

    ### URL的三个JS编码函数:`escape()`, `encodeURI()`, `encodeURIComponent()` 简介 在Web开发中,经常遇到的一个问题是URL传递中文字符时出现乱码的情况。为了解决这一问题,JavaScript提供了几种不同的编码方法...

    java中文乱码之解决URL中文乱码问题的方法

    - `escape()` 方法:它将非ASCII字符转换为%xx格式,但并不直接适用于URL编码,因为它不会编码一些特殊字符,如“+”。 - `encodeURI()` 方法:此方法针对整个URL进行编码,使用UTF-8编码,但不编码某些特殊字符,...

    谈谈encodeURI和encodeURIComponent以及escape的区别与应用

    在JavaScript中,`encodeURI()`、`encodeURIComponent()`和`escape()`是三个用于字符串编码的函数,它们的主要目的是确保特殊字符在传输过程中不会引起错误。理解它们之间的差异对于编写正确处理URL和数据的...

    escape、encodeURI 和 encodeURIComponent 的区别

    在JavaScript中,`escape()`, `encodeURI()`, 和 `encodeURIComponent()` 是三个常见的字符串编码函数,它们用于处理和转换字符串中的特殊字符。虽然它们都与字符串编码有关,但各自的作用和适用场景有所不同。 ...

    Javascript中escape(),_encodeURI()和encodeURIComponent()之精析与比较.doc

    在JavaScript中,`escape()`, `encodeURI()`, 和 `encodeURIComponent()` 是三个常见的字符串编码函数,它们各自有独特的用途和特点。理解它们的区别对于编写健壮的JavaScript代码至关重要。 首先,`escape()` 函数...

    javascript 对url编码 解码

    JavaScript 中对 URL 编码和解码涉及到六个函数:escape、encodeURI、encodeURIComponent、unescape、decodeURI 和 decodeURIComponent。这六个函数的使用场景和特点分别是: escape 函数:escape 函数用于将字符串...

    简单明了区分escape、encodeURI和encodeURIComponent

    本文主要关注三个与URL编码相关的函数:`escape`、`encodeURI`和`encodeURIComponent`。这些函数各有其特定的用途,理解它们的区别至关重要。 首先,我们来看`escape`函数。`escape`并不专门用于URL编码,它实际上...

    javascript 字符 Escape,encodeURI,encodeURIComponent

    JavaScript中的字符串编码转换是开发过程中不可或缺的一部分,尤其是在处理URL、查询参数或特殊字符时。本文将详细介绍`escape()`、`encodeURI()`和`encodeURIComponent()`这三个函数,并解释它们的区别和使用场景。...

    js中字符串编码函数escape()、encodeURI()、encodeURIComponent()区别详解

    JavaScript中的字符串编码函数主要包括`escape()`、`encodeURI()`和`encodeURIComponent()`,它们的作用是对字符串进行编码处理,以便在网络中安全地传输数据。这三种函数都有对应的解码函数:`unescape()`、`...

    JavaScript encodeURI 和encodeURIComponent

    encodeURI和encodeURIComponet函数都是javascript中用来对URI进行编码,将相关参数转换成UTF-8编码格式的数据。URI在进行定位跳转时,参数里面的中文、日文等非ASCII编码都会进行编码转换

    js中编码函数:escape,encodeURI与encodeURIComponent详解

    而他们之间的异同却困扰了很多的Javascript初学者,这篇文章详细的给大家介绍了js中编码函数:escape,encodeURI与encodeURIComponent的相关资料,需要的朋友可以参考下。

    ZzJavaScript encode and escape functions

    JavaScript中的编码和转义函数主要有encodeURI、encodeURIComponent和escape这三个函数,它们用于处理URL和URI中的特殊字符。下面将详细介绍这三个函数的功能和使用场景。 首先,encodeURI函数的主要用途是编码整个...

Global site tag (gtag.js) - Google Analytics