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

javascript URL编码和解码使用说明_javascript技巧

May 16, 2016 pm 06:29 PM
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) 举例: 输出结果如下: 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

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1319
25
PHP教程
1269
29
C# 教程
1248
24
如何解決Java開發中的URL解碼異常 如何解決Java開發中的URL解碼異常 Jun 29, 2023 pm 02:07 PM

如何解決Java開發中的URL解碼異常在Java開發中,我們經常會遇到需要解碼URL的情況。然而,由於不同的編碼方式或不規範的URL字串,有時會出現URL解碼異常的情況。本文將介紹一些常見的URL解碼異常以及對應的解決方法。一、URL解碼異常的產生原因編碼方式不符:URL中的特殊字元需要進行URL編碼,即將轉換為以%開頭的十六進位值。解碼時,需要使

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字串。本文將透過世代

使用Python進行Base64編碼和解碼 使用Python進行Base64編碼和解碼 Sep 02, 2023 pm 01:49 PM

假設您有一個想要透過網路傳輸的二進位影像檔案。您很驚訝對方沒有正確接收該文件-該文件只是包含奇怪的字元!嗯,您似乎試圖以原始位元和位元組格式發送文件,而所使用的媒體是為串流文字而設計的。避免此類問題的解決方法是什麼?答案是Base64編碼。在本文中,我將向您展示如何使用Python對二進位影像進行編碼和解碼。該程式被說明為一個獨立的本地程序,但您可以將該概念應用於不同的應用程序,例如將編碼圖像從行動裝置發送到伺服器以及許多其他應用程式。什麼是Bas​​e64?在深入了解本文之前,讓我們先定義一下Base6

如何利用PHP函數進行URL編碼和解碼? 如何利用PHP函數進行URL編碼和解碼? Jul 24, 2023 pm 11:21 PM

如何利用PHP函數進行URL編碼和解碼?在PHP中,URL編碼和解碼是非常常見的操作。 URL編碼是將URL中的特殊字元轉換為對應的編碼值,常見的特殊字元包括空格、斜線、問號等。而URL解碼則是將編碼值轉換回原始的特殊字元。 PHP提供了一系列函數來實作URL編碼和解碼的功能,本文將介紹常用的urlencode()和urldecode()函數,並給出對應的程式碼示

如何在C語言程式設計中實作中文字元的編碼和解碼? 如何在C語言程式設計中實作中文字元的編碼和解碼? Feb 19, 2024 pm 02:15 PM

在現代電腦程式設計中,C語言是一種非常常用的程式語言之一。儘管C語言本身並不直接支援中文編碼和解碼,但我們可以使用一些技術和函式庫來實現這項功能。本文將介紹如何在C語言程式設計軟體中實作中文編碼和解碼。首先,要實作中文編碼和解碼,我們需要了解中文編碼的基本概念。目前,最常用的中文編碼方案是Unicode編碼。 Unicode編碼為每個字元分配了一個唯一的數字值,以便在計

使用PHP中的urlencode()函數對URL進行編碼 使用PHP中的urlencode()函數對URL進行編碼 Nov 18, 2023 am 08:53 AM

使用PHP中的urlencode()函數對URL進行編碼的具體程式碼範例如下:&lt;?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

Python中的位元組編碼和解碼技巧有哪些? Python中的位元組編碼和解碼技巧有哪些? Oct 18, 2023 am 09:27 AM

Python中的位元組編碼和解碼技巧有哪些?位元組編碼和解碼是我們在處理文字資料時常常遇到的問題。在Python中,有許多內建的函數和模組可以幫助我們進行位元組編碼和解碼操作。本文將介紹幾種常見的位元組編碼和解碼技巧,並給出對應的程式碼範例。使用encode()函數進行位元組編碼encode()函數是Python中用於將Unicode字串編碼為位元組序列的方法。它的一般

See all articles