Global Objects
Global Objects
Global Functions
encodeURIComponent
The encodeURIComponent() global function is used to encode a URI component str. It escapes certain special characters into their corresponding percent-encoded (%) sequences based on UTF-8 encoding. This ensures that the component can be correctly interpreted when used as part of a URL, especially in query string parameters, paths, or fragments.
Letters, numbers, and - _ . ! ~ * ' ( ) are not encoded. Other characters are encoded into percent-escape sequences (e.g., a space is encoded as %20).
encodeURIComponent() behaves identically to the function of the same name in the Web.
Example:
console.log(encodeURIComponent("https://example.com/page?id=100"));
// output: https%3A%2F%2Fexample.com%2Fpage%3Fid%3D100
decodeURIComponent
The decodeURIComponent() global function is used to decode a URI component str encoded by encodeURIComponent(). It converts percent-encoded (%) sequences back to their original character forms, thereby restoring the original URI component. For example, it converts %20 back to a space.
decodeURIComponent() behaves identically to the function of the same name in the Web.
Example:
console.log(decodeURIComponent("https%3A%2F%2Fexample.com%2Fpage%3Fid%3D100"));
// output: https://example.com/page?id=100
URI
This function accepts a string and parses it into a Uri object for subsequent processing. The uri parameter is the URI string to be parsed.
The return value is an object containing the following fields:
scheme: string: The scheme field parsed from the parameter;authority: string: The authority field parsed from the parameter;path: string: The path field parsed from the parameter;query: string: The query field parsed from the parameter;origin: string: The original URI string in the parameter;toString: () => string: This method can re-encode this object into a URI string.
For example:
console.log(URI("https://app-name/icon.png"))
// {
// scheme: 'https',
// authority: 'app-name',
// path: '/icon.png',
// query: '',
// origin: 'https://app-name/icon.png',
// toString: <function>
// }
The URI function also accepts an object as a parameter. In this case, the URI function adds a toString method to the parameter object, which can be used to encode the URI object into a string:
let uri = {
scheme: 'https',
authority: 'app-name',
path: '/icon.png',
query: ''
}
console.log(URI(uri).toString()) // 'https://app-name/icon.png'
