`

javascript的URL编码和解码

阅读更多
在使用url进行参数传递时,经常会传递一些中文名的参数或URL地址,在后台处理时会发生转换错误。在有些传递页面使用GB2312,而在接收页面使用UTF8,这样接收到的参数就可能会与原来发生不一致。使用服务器端的urlEncode函数编码的URL,与使用客户端javascript的 encodeURI函数编码的URL,结果就不一样。
javaScript中的编码方法:
escape() 方法:
采用ISO Latin字符集对指定的字符串进行编码。所有的空格符、标点符号、特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符在字符集表里面的编码的16进制数字)。比如,空格符对应的编码是%20。unescape方法与此相反。不会被此方法编码的字符: @ * / +

英文解释: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."
Edge 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.


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

英文解释: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. Edge 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


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

英文解释: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. 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格式的(比如原页面和目标页面的charset是一致的时候),只需要使用escape。如果你的页面是GB2312或者其他的编码,而接受参数的页面是UTF-8编码的,就要采用 encodeURI或者encodeURIComponent。

       另外,encodeURI/encodeURIComponent是在javascript1.5之后引进的,escape则在javascript1.0版本就有。

英文注释:The escape() method does not encode the + character which is interpreted as a space on the server side as well as generated by forms with spaces in their fields. Due to this shortcoming, you should avoid use of escape() whenever possible. The best alternative is usually encodeURIComponent().Use of the encodeURI() method is a bit more specialized than escape() in that it encodes for URIs [REF] as opposed to the querystring, which is part of a URL. Use this method when you need to encode a string to be used for any resource that uses URIs and needs certain characters to remain un-encoded. Note that this method does not encode the ' character, as it is a valid character within URIs.Lastly, the encodeURIComponent() method should be used in most cases when encoding a single component of a URI. This method will encode certain chars that would normally be recognized as special chars for URIs so that many components may be included. Note that this method does not encode the ' character, as it is a valid character within URIs.
1.编码处理函数
1) encodeURI 返回一个对URI字符串编码后的结果。URL是最常见的一种URI;
2) decodeURI 将一个已编码的URI字符串解码成最原始的字符串返回;
3) 举例: < Script language = " javascript " > 输出结果如下: encodeStr: http://www.amigoxie.com/index.jsp?name=%E9%98%BF%E8%9C%9C%E6%9E%9C decodeStr: http://www.amigoxie.com/index.jsp?name=xind
2. 数值处理函数
1) parseInt 将一个字符串指定的进制转换为一个整数,语法格式为: parseInt(numString, [radix]) 第一个参数是要进行转换的字符串,是介于2到36之间的数值,用于指定进行字符串转换时所用的进制。 举例如下: 输出结果如下:默认情况下的结果:32:32;032:26;0x32:50 转为2进制的结果:32:NaN;032:0;0x32:0 转为8进制的结果:32:26;032:26;0x32:0 转为16进制的结果:32:50;032:50;0x32:50 11001010转换后的结果: 2进制:202;16进制:285216784 8进制:2359816;10进制:11001010 43abc转换后:43;abc43转换后:NaN;abc转换后:NaN
2) parseFloat方法 该方法将一个字符串转换成对应的小数。 eg. 输出结果如下: 4.11 5.1 3) isNaN方法 该方法用于检测前两个方法返回值是否为非数值型,如果是,返回true,否则,反回false
分享到:
评论

相关推荐

    javascript URL编码和解码使用说明

    本文将详细介绍JavaScript中常见的URL编码与解码方法。 首先,我们来了解`escape()`方法。这个方法可以将字符串按照ISOLatin字符集进行编码。编码过程中,所有的空格、标点符号、特殊字符以及其他非ASCII字符会被...

    javascript 对url编码 解码

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

    urlcode解码-HTTP:URL编码解码

    在实际开发中,了解并正确使用URL编码和解码不仅能保证数据的完整性和安全性,还能防止URL重定向和路由处理中的问题。因此,掌握这个基本概念对于任何从事Web开发的人员都是至关重要的。 最后,我们注意到压缩包中...

    url编码解码源代码

    本话题将详细探讨“url编码解码源代码”,以及如何在CGI(Common Gateway Interface)环境中处理这些编码。 首先,让我们理解URL编码的基本原理。在URL中,某些字符具有特殊含义,如空格通常代表路径分隔,而问号...

    纯Javascript脚本实现GBK URL编解码

    总结来说,纯JavaScript实现GBK URL编解码涉及到的关键技术点包括GBK编码和解码、URL编码和解码,以及可能的Base64编码和解码。实际实现时,可能需要结合第三方库或自定义函数来处理GBK编码,同时充分利用JavaScript...

    URL编码解码器

    URL编码(URL Encoding)是...了解并掌握URL编码解码对于理解和开发Web应用程序,特别是前端和服务器端交互的环节,是十分必要的。通过"URL编码解码器"这样的工具,开发者可以更方便地处理这些细节问题,提高工作效率。

    一个简单url编码解码

    这个名为“一个简单url编码解码”的项目,就是为新手提供了一个理解和实践URL编码与解码机制的实例。 URL编码是根据RFC 3986标准进行的,主要使用百分号(%)表示非ASCII字符或特殊字符的ASCII十六进制值。例如,...

    codeURL编码解码工具

    本文将详细讲解"codeURL编码解码工具"的相关知识点,包括URL编码的原理、用途以及如何使用这类工具进行编码和解码。 URL(Uniform Resource Locator)即统一资源定位符,是互联网上的资源地址。它由协议类型、主机...

    URL解码-编码器URL解码-编码器

    在实际操作中,开发者通常会使用编程语言提供的内置函数进行URL编码和解码,例如JavaScript的`encodeURIComponent()`和`decodeURIComponent()`,Python的`urllib.parse.quote()`和`urllib.parse.unquote()`,Java的`...

    javascript编码与解码

    JavaScript提供了`decodeURI()`和`decodeURIComponent()`函数来处理URL编码的字符串,而`TextDecoder` API则可以解码非UTF-8的编码格式。 `TextDecoder`是一个强大的工具,它可以解码多种编码格式,如GBK、Big5等。...

    URL编码解码工具 反向分析URL地址

    URL编码与解码是互联网通信中的重要环节,它涉及到数据在网络中传输时的标准化..."URL编码解码工具.exe" 提供了一个直观、便捷的方式来操作和分析URL,对于IT从业者,尤其是Web开发者来说,是一个非常实用的辅助工具。

    UrlEncodeDecode解码编码工具(双向)

    本工具旨在提供一个方便、高效的解决方案,支持URL编码和解码的双向操作。 URL编码,也称为Percent-encoding,是URI(统一资源标识符)的一部分,用于在不安全或非ASCII字符出现在URL中时进行转换。这是因为URL格式...

    Encode_ Decode Tools URL编码解码工具

    `Encode_ Decode Tools`是一款用于URL编码和解码的工具,它可以帮助开发者和用户方便地处理这些编码问题。这款工具可能提供了简单的用户界面,允许用户输入URL或字符串,然后点击按钮进行编码或解码操作。它支持对...

    JS实现URL编码转换中文

    在JavaScript中,提供了`encodeURIComponent`和`decodeURIComponent`两个方法来进行URL编码与解码操作。 - **`encodeURIComponent`**:该函数接受一个字符串作为参数,并返回一个经过URL编码的字符串。 - **`...

    URL编码 URL编码

    在实际编程中,大多数编程语言都提供了内置函数来自动进行URL编码和解码,如JavaScript的`encodeURIComponent()`和`decodeURIComponent()`,Python的`urllib.parse.quote()`和`urllib.parse.unquote()`等。...

    javascript 编码解码(只针对英文字符)

    通过分析这个文件,我们可以进一步了解实际的编码解码过程和应用场景。 总之,JavaScript提供了多种处理字符串编码和解码的方法,对于英文字符,主要使用ASCII编码,而对于包含多语言的字符串,则需要借助Unicode和...

    网页链接 URL 地址解码

    理解URL编码和解码的工作原理,掌握相关工具的使用,能帮助我们更好地理解和解决与URL相关的问题,提高网络交互的效率和体验。无论是日常浏览网页还是进行Web开发,都需要对这一概念有清晰的认识。

    实现HTML编码和解码的JavaScript工具类

    JavaScript作为一种客户端脚本语言,提供了处理这种编码和解码的功能,尤其在动态生成HTML内容时。"js-htmlencode"就是这样一个专为此目的设计的工具类。 该工具类主要包含以下功能: 1. HTML编码:将HTML特殊字符...

    Delphi Base64编码,javascript解码汉字乱码解决方法演示程序(源代码)

    解决思路:进行Base64前先进行URL编码,在进行URL编码的时候,注意设置不需要SpaceAsPlus选项。 javascript代码: let decodedData = window.atob(JSONStr); let decodedData1 = decodeURIComponent(decodedData)...

Global site tag (gtag.js) - Google Analytics