How does javascript implement URL transcoding and decoding?
The content of this article is about how JavaScript implements URL transcoding and decoding. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1. escape and unescape
escape() cannot be used directly for URL encoding. Its real function is to return the Unicode encoding value of a character.
Use the unicode character set to encode the specified string except 0-255. All spaces, punctuation marks, special characters and more related non-ASCII characters will be converted into character encoding in %xx format (xx is equal to the hexadecimal number of the character encoding in the character set table). For example, the encoding corresponding to the space character is .
Escape does not encode 69 characters: *, , -, ., /, @, _, 0-9, a-z, A-Z.
The escape() function is used to encode strings in js and is not commonly used.
Encoding:
escape('http://www.baidu.com?name=zhang@xiao@jie&order=1')
Result: "http://www.baidu.com?name=zhang@xiao@jie&order=1"
escape('Zhang')
Result: "%u5F20"
Decoding:
unescape("http%3A//www.baidu.com%3Fname%3Dzhang@xiao@jie%26order%3D1")
Result: "http://www.baidu.com?name=zhang@xiao@jie&order=1"
unescape("%u5F20")
Result: "Zhang"
2. encodeURI and decodeURI
encode the URI string in UTF-8 The format is converted into escape various strings.
encodeURI does not encode 82 characters: !, #, $, &, ', (,), *, ,,, -,.,/,:,;,=,?,@,_,~, 0-9, a-z, A-Z
encodeURI() is used to encode the entire url
Encoding:
encodeURI('http://www.baidu.com?name=zhang@xiao@jie&order=1')
Result: "http://www.baidu.com?name =zhang@xiao@jie&order=1"
Decoding:
decodeURI("http%3A//www.baidu.com%3Fname%3Dzhang@xiao@jie%26order%3D1")
Result: "http://www.baidu.com?name=zhang@xiao@jie&order=1"
3. encodeURIComponent and decodeURIComponent
The difference from encodeURI() is that it is used to encode individual components of the URL rather than encoding the entire URL.
therefore,"; / ? : @ & = $ , #", these symbols that are not encoded in encodeURI() will all be encoded in encodeURIComponent(). As for the specific encoding method, both are the same. Convert the URI string into escape format using UTF-8 encoding format String.
encodeURIComponent() is used to pass parameters. Parameters containing special characters may cause discontinuities.
Encoding:
encodeURIComponent('http://www.baidu.com?name=zhang@xiao@jie&order=1')
Result: "http:// www.baidu.com?name=zhang@xiao@jie&order=1"
Decoding:
decodeURIComponent("http%3A%2F%2Fwww.baidu.com%3Fname%3Dzhang%40xiao%40jie%26order%3D1")
Result:"http://www.baidu.com?name=zhang @xiao@jie&order=1"
Summary:
escape() cannot be used directly for URL encoding. Its real function is to return the Unicode encoding value of a character. For example, the return of "Spring Festival" The result is %u6625%u8282,, escape() is incorrect" "Encoding
is mainly used for Chinese character encoding, and its use is no longer recommended.
encodeURI() is actually used to encode URLs in Javascript Encoding function.
Encodes the entire URL address, but does not encode the symbols with special meanings "; / ? : @ & = $ , #". The corresponding decoding function is: decodeURI().
encodeURIComponent()
Can encode the special characters "; / ? : @ & = $ , #". The corresponding decoding function is decodeURIComponent().
I want to pass the URL with ampersand , so use encodeURIComponent()
The above is the detailed content of How does javascript implement URL transcoding and decoding?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

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

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest
