This article is a mirror article of machine translation, please click here to jump to the original article.

View: 12798|Reply: 2

[JavaScript] Encoding URLs and parameters in JavaScript

[Copy link]
Posted on 4/16/2022 6:50:39 PM | | | |
URL encoding

Some characters can't appear in URLs (e.g. spaces), others have special meanings in URLs. We need to convert special characters to conform to the syntax defined in the W3 URI specification (RFC 3986). This means that URLs must contain only a subset of a special ASCII character: the letters and numbers we are familiar with, as well as some reserved characters that are used as control characters in the URL.

URL encoding processing in JavaScript

In JavaScript, both encodeURIComponent() and encodeURI() built-in functions can be URL encoded, replacing the reserved characters in the URL with their UTF-8 encoded characters.

Most browsers automatically encode URLs, such as encoding spaces to %20, but there are always some exceptions. Fortunately, browsers provide built-in URL encoding and decoding functions: encodeURI, encodeURIComponent and decodeURI, decodeURIComponent. The question is how to use these functions, and what is the difference between them.

encodeURI()

URL encoding a string is easy, just call the encodeURI and pass in the string you want to encode. This function returns the encoded URL.

decodeURI()

This function decodes and calls the decodeURI function, passing in the encoded string, and then it returns the decoded normal string.



encodeURIComponent()

The encodeURIComponent function should be used to encode the parameter values of the query string. The difference between encodeURI and encodeURIComponent is that encodeURIComponent encodes the entire string, while encodeURI ignores the protocol prefix ('http://') as well as the domain name. encodeURIComponent is designed to encode everything, while encodeURI ignores the domain name part of the URL. When youWhen you want to encode the parameter value in the URL, select the encodeURIComponent function

decodeURIComponent()

The decodeURIComponent function will decode the encoded string of the encodeURIComponent function and restore it to its normal content.



Review:

JS encodes and decodes URLs (three ways to distinguish them)
https://www.itsvse.com/thread-6844-1-1.html

URL encoding and decoding
https://www.itsvse.com/thread-2728-1-1.html

URL encoding and decoding in ASP.NET
https://www.itsvse.com/thread-2642-1-1.html






Previous:SQL Server time interval priority selection
Next:jQuery hide doesn't work two solutions
Posted on 2/8/2023 10:07:55 PM |
Mark the get request URL with # special character, automatically intercept the content after #, resulting in an incorrect URL address and cannot be accessed normally
Posted on 12/21/2023 10:22:07 AM |
Prompt:Authors are banned or removed content is automatically blocked
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com