The URIs are in the following order
- What is a Uniform Resource Identifier?
- How urIs relate to urls and UrNs?
- Composition of a URI.
- EncodeURI (), encodeURIComponent (), decodeURI(), decodeURIComponent() use and difference
- Percent code
What is a Uniform Resource Identifier?
In computer parlance, a Uniform Resource Identifier (URI) is a string of characters used to identify the name of an Internet Resource.
How urIs relate to urls and UrNs?
Look at the definition
- A Uniform Resource Identifier (URI) is a string that identifies the name of an Internet Resource. This identifier allows users to interact with resources on the network (commonly referred to as the World Wide Web) over specific protocols.
- Uniform Resource Locator (URL), the most common form of which is a URI, is often specified as an informal web address.
- URN (Uniform Resource Name), whose purpose is to complement urls by providing a way to identify resources in a particular namespace.
The URL(locator) and URN(name) schemes are subclasses of URIs, which can be either URLS or URNs. Technically, urls and UrNs are resource ids; However, it is often impossible to categorize a scheme into one of the two: all URIs can be treated as names, and some schemes embody different parts of both
The composition of the URI
The URI grammar consists of the URI protocol name (such as HTTP, FTP, mailto, file), a colon, and the corresponding content of the protocol. A specific protocol defines the syntax and semantics of the protocol content, and all protocols must follow certain general rules of URI grammar, that is, some special characters are reserved for some special purposes. URI grammas also impose additional restrictions on protocol content for a variety of reasons, such as ensuring interoperability between layered protocols. The percent encoding also provides additional information for the URI.
The format of a generic URI is as follows:
Protocol name :// host name: port/path? Query parameter # fragment ID
The following figure shows two EXAMPLES of URIs and their components.
EncodeURI (), encodeURIComponent (), decodeURI(), decodeURIComponent() use and difference
The Global object is one of the most special objects in ECMAScript, because no matter how you look at it, it doesn’t exist. The ECMAScript Global object is defined in a sense as the ultimate “back-of-the-envelope object.” In other words, properties and methods that do not belong to any other object are ultimately its properties and methods. In fact, there are no global variables or functions; All properties and functions defined in the Global scope are properties of the Global object.
- The encodeURI() and encodeURIComponent() methods of the Global object can encode URIs (Uniform Resource Identifiers) for sending to the browser. Valid URIs cannot contain certain characters, such as Spaces. These two URI encodings encode URIs by replacing all invalid characters with special UTF-8 encodings that browsers can accept and understand. Where encodeURI() is used primarily for the entire URI(for example, www.wrox.com/illegal value.htm), EncodeURIComponent () is used to encode a section of the URI, such as illegal value.htm. The main difference is that encodeURI() does not encode special characters that are urIs themselves, such as colons, forward slashes, question marks, and hash points; EncodeURIComponent () encodes any nonstandard characters it finds. Consider the following example.
var uri = "http://www.wrox.com/illegal value.htm#start";
//"http://www.wrox.com/illegal%20value.htm#start"
alert(encodeURI(uri));
//"http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start"
alert(encodeURIComponent(uri));
Copy the code
The result of encodeURI() is that all characters except Spaces are left intact, with Spaces replaced with %20. The encodeURIComponent() method replaces all non-alphanumeric characters with the corresponding encoding. This is why you can use encodeURI() for entire URIs, and encodeURIComponent() for strings appended to existing URIs.
In general, we use the encodeURIComponent() method more often than encodeURI(), because in practice it is more common to encode query string parameters rather than the underlying URI.
The encodeURI() and encodeURIComponent() methods correspond to decodeURI() and decodeURIComponent(). DecodeURI () decodes only characters replaced with encodeURI(). For example, it can replace %20 with a space, but does nothing to %23 because %23 represents the hash number (#), which is not replaced with encodeURI(). Similarly, decodeURIComponent() can decode all characters encoded with encodeURIComponent(), that is, it can decode any special character encoding. Consider the following example:
var uri = "http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start";
//http%3A%2F%2Fwww.wrox.com%2Fillegal value.htm%23start
alert(decodeURI(uri));
//http://www.wrox.com/illegal value.htm#start
alert(decodeURIComponent(uri));
Copy the code
Here, the variable URI contains a string encoded by encodeURIComponent(). In the result of the first call to decodeURI(), only %20 is replaced with Spaces. The second call to decodeURIComponent() results in an unescaped string (but not a valid URI) with the encoding of all the special characters replaced by the original characters.
The URI methods encodeURI(), encodeURIComponent(), decodeURI(), and decodeURIComponent() are used to replace the escape() and unescape() methods deprecated by ECMA-262 version 3. The URI method can encode all Unicode characters, whereas the original method can only encode ASCII characters correctly. Therefore, in development practice, especially in production-level code, it is important to use URI methods rather than escape() and unescape().
Percent code
Percent-encoding, also known as URL encoding, is a context-specific encoding mechanism for Uniform resource Locator (URL), which also applies to uniform Resource Identifier (URI) encoding. Also used for application/ X-www-form-urlencoded MIME(preparing data, as it is used to submit HTML form data via HTTP request operations (requests).
The percent encoding of the URI
The character type of the URI
Characters allowed by urIs are reserved and unreserved. Reserved characters are characters that have special meanings, such as: slash characters used to delimit different parts of A URL (or URI); Unreserved characters do not have these special meanings. Percent encoding represents reserved characters as special character sequences. This varies slightly from URI to URI version of specification.
! |
* |
' |
( |
) |
; |
: |
@ |
& |
= |
+ |
$ |
. |
/ |
? |
# |
[ |
] |
---|
A |
B |
C |
D |
E |
F |
G |
H |
I |
J |
K |
L |
M |
N |
O |
P |
Q |
R |
S |
T |
U |
V |
W |
X |
Y |
Z |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
a |
b |
c |
d |
e |
f |
g |
h |
i |
j |
k |
l |
m |
n |
o |
p |
q |
r |
s |
t |
u |
v |
w |
x |
y |
z |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
- |
_ |
. |
~ |
Other characters in the URI must be encoded by a percentage sign.
Percentage encoding of reserved characters
If a reserved character has a special meaning in a particular context (called “reserved purpose”) and must be used in the URI for other purposes, it must be percent-encoded. The percent sign encodes a reserved character by first representing the ASCII value of the character as two hexadecimal numbers, and then preceding it with an escape character (“%”), placed in the appropriate position in the URI. (For non-ASCII characters, you need to convert to UTF-8 byte order, and then each byte is represented as above.)
! |
# |
$ |
& |
' |
( |
) |
* |
+ |
. |
/ |
: |
; |
= |
? |
@ |
[ |
] |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
% 21 |
% 23 |
% 24 |
% 26 |
% 27 |
% 28 |
% 29 |
%2A |
%2B |
%2C |
%2F |
%3A |
%3B |
%3D |
%3F |
% 40 |
%5B |
%5D |
A reserved character that has no special meaning in a particular context can also be encoded by a percentage sign, semantically indistinguishable from a character that does not have a percentage sign.
In the URI “query” query string (page does not exist)”)” component (? Characters such as “/” are still reserved characters but have no special meaning unless a particular URI specifies otherwise. The/character does not require percent encoding if it has no special meaning.
If a reserved character has a special meaning, the URI encoded by a percentage sign for the reserved character has different semantics than the URI represented by the reserved character in its own right.