搜尋
首頁web前端js教程javascript URL编码和解码使用说明_javascript技巧

在有些传递页面使用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) 举例: 输出结果如下: 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

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
使用Python进行Base64编码和解码使用Python进行Base64编码和解码Sep 02, 2023 pm 01:49 PM

假设您有一个想要通过网络传输的二进制图像文件。您很惊讶对方没有正确接收该文件-该文件只是包含奇怪的字符!嗯,您似乎试图以原始位和字节格式发送文件,而所使用的媒体是为流文本而设计的。避免此类问题的解决方法是什么?答案是Base64编码。在本文中,我将向您展示如何使用Python对二进制图像进行编码和解码。该程序被说明为一个独立的本地程序,但您可以将该概念应用于不同的应用程序,例如将编码图像从移动设备发送到服务器以及许多其他应用程序。什么是Base64?在深入了解本文之前,让我们先定义一下Base6

如何解决Java开发中的URL解码异常如何解决Java开发中的URL解码异常Jun 29, 2023 pm 02:07 PM

如何解决Java开发中的URL解码异常在Java开发中,我们经常会遇到需要解码URL的情况。然而,由于不同的编码方式或者不规范的URL字符串,有时候会出现URL解码异常的情况。本文将介绍一些常见的URL解码异常以及对应的解决方法。一、URL解码异常的产生原因编码方式不匹配:URL中的特殊字符需要进行URL编码,即将其转换为以%开头的十六进制值。解码时,需要使

如何在C语言编程中实现中文字符的编码和解码?如何在C语言编程中实现中文字符的编码和解码?Feb 19, 2024 pm 02:15 PM

在现代计算机编程中,C语言是一种非常常用的编程语言之一。尽管C语言本身并不直接支持中文编码和解码,但我们可以使用一些技术和库来实现这一功能。本文将介绍如何在C语言编程软件中实现中文编码和解码。首先,要实现中文编码和解码,我们需要了解中文编码的基本概念。目前,最常用的中文编码方案是Unicode编码。Unicode编码为每个字符分配了一个唯一的数字值,以便在计

Python 3.x 中如何使用urllib.parse.quote()函数对URL进行编码Python 3.x 中如何使用urllib.parse.quote()函数对URL进行编码Jul 31, 2023 pm 10:46 PM

Python3.x中使用urllib.parse.quote()函数对URL进行编码在网络应用开发中,经常会遇到需要对URL进行编码的情况,这是由于URL中允许的字符有限,而我们需要传递的参数可能包含了特殊字符。Python中的urllib.parse模块提供了quote()函数,可以对URL中的非法字符进行编码,使之成为合法的URL字符串。本文将通过代

如何利用PHP函数进行URL编码和解码?如何利用PHP函数进行URL编码和解码?Jul 24, 2023 pm 11:21 PM

如何利用PHP函数进行URL编码和解码?在PHP中,URL编码和解码是非常常见的操作。URL编码是将URL中的特殊字符转换为相应的编码值,常见的特殊字符包括空格、斜杠、问号等。而URL解码则是将编码值转换回原始的特殊字符。PHP提供了一系列函数来实现URL编码和解码的功能,本文将介绍常用的urlencode()和urldecode()函数,并给出相应的代码示

Python 3.x 中如何使用urllib.parse.urlencode()函数对参数进行编码Python 3.x 中如何使用urllib.parse.urlencode()函数对参数进行编码Aug 03, 2023 pm 03:18 PM

Python3.x中如何使用urllib.parse.urlencode()函数对参数进行编码在进行网络编程或者进行HTTP请求时,往往需要对URL中的参数进行编码。而Python中的urllib模块提供了方便的urlencode()函数来进行URL参数的编码。本文将介绍如何在Python3.x中使用urllib.parse.urlencode()函

使用PHP中的urlencode()函数对URL进行编码使用PHP中的urlencode()函数对URL进行编码Nov 18, 2023 am 08:53 AM

使用PHP中的urlencode()函数对URL进行编码的具体代码示例如下:<?php//定义要编码的URL$url="https://www.example.com/search?q=一个中文查询";//对URL进行编码$encodedUrl=urlencode($url);echo"编码前

Python 2.x 中如何使用urllib.quote()函数对URL进行编码Python 2.x 中如何使用urllib.quote()函数对URL进行编码Jul 31, 2023 pm 08:37 PM

Python2.x中如何使用urllib.quote()函数对URL进行编码URL中包含了多种字符,包括字母、数字、特殊字符等。为了使URL能够正确地传输和解析,我们需要对其中的特殊字符进行编码。在Python2.x中,可以使用urllib.quote()函数对URL进行编码,下面我们来详细介绍其用法。urllib.quote

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境