Home Web Front-end JS Tutorial Instructions for using javascript URL encoding and decoding_javascript skills

Instructions for using javascript URL encoding and decoding_javascript skills

May 16, 2016 pm 06:29 PM
url encoding decoding

In some delivery pages, GB2312 is used, while UTF8 is used in the receiving page, so the received parameters may be inconsistent with the original ones. The results are different between URLs encoded using the server-side urlEncode function and URLs encoded using the client-side JavaScript's encodeURI function.
Encoding method in javaScript:
escape() method:
Using the ISO Latin character set to encode the specified string. All spaces, punctuation marks, special characters and other non-ASCII characters will be converted into character encoding in %xx format (xx is equal to the hexadecimal number of the character's encoding in the character set table). For example, the encoding corresponding to the space character is . The unescape method is the opposite. Characters that will not be encoded by this method: @ * /

English explanation: 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 " ."
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() method : Convert the URI string into an escape format string using UTF-8 encoding format. Characters that will not be encoded by this method: ! @ # $& * ( ) = : / ; ? '

English explanation: 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() method : Convert the URI string into an escape format string using UTF-8 encoding format. Compared with encodeURI(), this method will encode more characters, such as / and other characters. Therefore, if the string contains several parts of the URI, you cannot use this method to encode, otherwise the URL will display an error after the / character is encoded. Characters that will not be encoded by this method: ! * ( )

English explanation: 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.


Therefore, for Chinese strings, if you do not want to convert the string encoding format into UTF-8 format (such as the original page and target When the charset of the page is consistent), you only need to use escape. If your page is GB2312 or other encoding, and the page that accepts parameters is UTF-8 encoded, you must use encodeURI or encodeURIComponent.
In addition, encodeURI/encodeURIComponent was introduced after javascript1.5, and escape was available in javascript1.0.

English note: 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. Encoding processing Function
1) encodeURI returns a result of encoding the URI string.URL is the most common URI;
2) decodeURI decodes an encoded URI string into the original string and returns it;
3) Example: < Script language = " javascript " > Output The results are as follows: encodeStr: http://www.amigoxie.com/index.jsp?name=amigoxie decodeStr: http://www.amigoxie.com/index.jsp?name=xind
2. Numerical processing Function
1) parseInt converts the base specified by a string into an integer. The syntax format is: parseInt(numString, [radix]) The first parameter is the string to be converted, which is between 2 and 36 The value between is used to specify the base used for string conversion. Examples are as follows: The output results are as follows: The default result: 32:32;032:26;0x32:50 The result converted to binary: 32:NaN;032:0;0x32:0 The result converted to octal :32:26;032:26;0x32:0 The result of conversion to hexadecimal: 32:50;032:50;0x32:50 11001010 The result after conversion: binary: 202; hexadecimal: 285216784 octal System: 2359816; Decimal: 11001010 43abc after conversion: 43; abc43 after conversion: NaN; after abc conversion: NaN
2) parseFloat method This method converts a string into the corresponding decimal. eg. The output results are as follows: 4.11 5.1 3) isNaN method This method is used to detect whether the return value of the first two methods is non-numeric. If so, return true, otherwise, return false

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
How to solve URL decoding exceptions in Java development How to solve URL decoding exceptions in Java development Jun 29, 2023 pm 02:07 PM

How to solve URL decoding exceptions in Java development. In Java development, we often encounter situations where URLs need to be decoded. However, due to different encoding methods or non-standard URL strings, URL decoding abnormalities sometimes occur. This article will introduce some common URL decoding exceptions and corresponding solutions. 1. The cause of URL decoding exception is mismatch in encoding methods: special characters in the URL need to be URL encoded, that is, converted into hexadecimal values ​​starting with %. When decoding, you need to use

How to use the urllib.parse.quote() function to encode URLs in Python 3.x How to use the urllib.parse.quote() function to encode URLs in Python 3.x Jul 31, 2023 pm 10:46 PM

Use the urllib.parse.quote() function to encode URLs in Python3. Parameters may contain special characters. The urllib.parse module in Python provides the quote() function, which can encode illegal characters in URLs into legal URL strings. This article will be passed through

Base64 encoding and decoding using Python Base64 encoding and decoding using Python Sep 02, 2023 pm 01:49 PM

Let's say you have a binary image file that you want to transfer over the network. You're surprised that the other party didn't receive the file correctly - it just contains weird characters! Well, it looks like you're trying to send the file in raw bits and bytes format, while the media you're using is designed for streaming text. What are the solutions to avoid such problems? The answer is Base64 encoding. In this article, I will show you how to encode and decode binary images using Python. The program is explained as a standalone local program, but you can apply the concept to different applications, such as sending encoded images from a mobile device to a server and many other applications. What is Base64? Before diving into this article, let’s define Base6

How to utilize PHP functions for URL encoding and decoding? How to utilize PHP functions for URL encoding and decoding? Jul 24, 2023 pm 11:21 PM

How to utilize PHP functions for URL encoding and decoding? In PHP, URL encoding and decoding are very common operations. URL encoding converts special characters in the URL into corresponding encoded values. Common special characters include spaces, slashes, question marks, etc. URL decoding converts the encoded value back to the original special characters. PHP provides a series of functions to implement URL encoding and decoding functions. This article will introduce the commonly used urlencode() and urldecode() functions and give corresponding code examples.

How to implement encoding and decoding of Chinese characters in C language programming? How to implement encoding and decoding of Chinese characters in C language programming? Feb 19, 2024 pm 02:15 PM

In modern computer programming, C language is one of the most commonly used programming languages. Although the C language itself does not directly support Chinese encoding and decoding, we can use some technologies and libraries to achieve this function. This article will introduce how to implement Chinese encoding and decoding in C language programming software. First, to implement Chinese encoding and decoding, we need to understand the basic concepts of Chinese encoding. Currently, the most commonly used Chinese encoding scheme is Unicode encoding. Unicode encoding assigns a unique numeric value to each character so that when calculating

Encode URL using urlencode() function in PHP Encode URL using urlencode() function in PHP Nov 18, 2023 am 08:53 AM

The specific code example of using the urlencode() function in PHP to encode a URL is as follows: &lt;?php//Define the URL to be encoded$url="https://www.example.com/search?q=A Chinese query ";//Encode the URL $encodedUrl=urlencode($url);echo" before encoding

How to use the urllib.quote() function to encode URLs in Python 2.x How to use the urllib.quote() function to encode URLs in Python 2.x Jul 31, 2023 pm 08:37 PM

How to use the urllib.quote() function to encode URLs in Python 2.x. URLs contain a variety of characters, including letters, numbers, special characters, etc. In order for the URL to be transmitted and parsed correctly, we need to encode the special characters in it. In Python2.x, you can use the urllib.quote() function to encode the URL. Let's introduce its usage in detail below. urllib.quote

What are the techniques for byte encoding and decoding in Python? What are the techniques for byte encoding and decoding in Python? Oct 18, 2023 am 09:27 AM

What are the techniques for byte encoding and decoding in Python? Byte encoding and decoding are problems we often encounter when processing text data. In Python, there are many built-in functions and modules that help us perform byte encoding and decoding operations. This article will introduce several common byte encoding and decoding techniques and give corresponding code examples. Byte Encoding Using the encode() Function The encode() function is a method in Python used to encode a Unicode string into a sequence of bytes. its general

See all articles