目录

@ohos.url (URL String Parsing)

URL is a uniform resource locator. This module provides common utility functions for parsing URL strings, constructing URL objects, and parsing and operating URL query parameters.

The module mainly contains the following core classes:

The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import { url } from '@kit.ArkTS';

URLParams9+

URLParams is a utility class for parsing, constructing, and manipulating URL parameters. This class provides a unified interface for handling URL query parameters.

[h2]constructor9+

constructor(init?: string[][] | Record<string, string> | string | URLParams)

The constructor of URLParams is used to create a URL parameter object, and is applicable to scenarios where URL query parameters need to be parsed, constructed, or manipulated.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Utils.Lang

Parameters

Name Type Mandatory Description
init string[][] | Record<string, string> | string | URLParams No

Input parameter object.

- string[][]: two-dimensional string array.

- Record<string, string>: object list.

- string: URL query parameter string.

- URLParams: URLParams instance object.

- Default value: null.

Example

// Construct a URLParams object in string[][] mode.
let objectParams = new url.URLParams([ ['user1', 'abc1'], ['query2', 'first2'], ['query3', 'second3'] ]);
// Construct a URLParams object in Record<string, string> mode.
let objectParams1 = new url.URLParams({'fod' : '1' , 'bard' : '2'});
// Construct a URLParams object in string mode.
let objectParams2 = new url.URLParams('?fod=1&bard=2');
// Construct a URLParams object using the search property of the url object.
let urlObject = url.URL.parseURL('https://developer.mozilla.org/?fod=1&bard=2');
let objectParams3 = new url.URLParams(urlObject.search);
// Construct a URLParams object using the params property of the url object.
let secondUrlObj = url.URL.parseURL('https://developer.mozilla.org/?fod=1&bard=2');
let objectParams4 = secondUrlObj.params;

[h2]append9+

append(name: string, value: string): void

Inserts a new key-value pair into the query string. Unlike the set method, append does not replace the value corresponding to an existing key name. Instead, it appends a new key-value pair, allowing multiple values to exist under the same key name. To replace an existing key value, use the set method.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Utils.Lang

Parameters

Name Type Mandatory Description
name string Yes Key of the key-value pair to append.
value string Yes Value of the key-value pair to append.

Example

// Parse the URL string.
let urlObject = url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2');
// Construct a URLParams object.
let paramsObject = new url.URLParams(urlObject.search.slice(1));
// Append a key-value pair.
paramsObject.append('fod', '3');

[h2]delete9+

delete(name: string): void

Deletes all key-value pairs with the specified name. If the specified name does not exist, no operation is performed.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Utils.Lang

Parameters

Name Type Mandatory Description
name string Yes Key name to delete.

Example

// Parse the URL string.
let urlObject = url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2');
// Construct a URLParams object.
let paramsObject = new url.URLParams(urlObject.search.slice(1));
// Delete the key-value pair with the specified name.
paramsObject.delete('fod');

[h2]getAll9+

getAll(name: string): string[]

Gets the set of all values corresponding to the specified name. If a key-value pair name that does not exist is searched for, the return value is an empty array.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Utils.Lang

Parameters

Name Type Mandatory Description
name string Yes Specified key name.

Return value

Type Description
string[] All the values obtained.

Example

// Parse the URL and construct a URLParams object.
let urlObject = url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2');
let params = new url.URLParams(urlObject.search.slice(1));
params.append('fod', '3'); // Append the second fod parameter value.
// Get all values of the specified name fod.
console.info(params.getAll('fod').toString()); // Output ["1","3"]

[h2]entries9+

entries(): IterableIterator<[string, string]>

Returns an iterator, where each item of the iterator is an Array. The first item of the Array is name, and the second item of the Array is value. This method behaves the same as [Symbol.iterator], both returning an iterator of key-value pairs.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Utils.Lang

Return value

Type Description
IterableIterator<[string, string]> Returns an iterator, where each item is a [string, string] array containing name and value.

Example

// Construct a URLParams object.
let paramsObject = new url.URLParams('keyName1=valueName1&keyName2=valueName2');
// Get the entries iterator.
let pair = paramsObject.entries();
// Iterate over key-value pairs.
for (let item of pair) {
  console.info(item[0] + '=' + item[1]);
}
// keyName1=valueName1
// keyName2=valueName2

[h2]forEach9+

forEach(callbackFn: (value: string, key: string, searchParams: URLParams) => void, thisArg?: Object): void

Traverses the key-value pairs on the URLParams instance object in insertion order through a callback function.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Utils.Lang

Parameters

Name Type Mandatory Description
callbackFn function Yes Callback function executed when traversing key-value pairs, called once for each key-value pair.
thisArg Object No Value of this to use when callbackFn is invoked. The default value is this object.

Table 1 callbackFn parameter description

Name Type Mandatory Description
value string Yes Value of the current traversal.
key string Yes Key that is currently traversed.
searchParams URLParams Yes Instance that invokes the forEach method.

Example

// Parse the URL.
const myURLObject = url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2');
// Traverse the URLParams key-value pairs through a callback function.
myURLObject.params.forEach((value, name, searchParams) => {
    console.info(name, value, myURLObject.params === searchParams);
});

[h2]get9+

get(name: string): string | null

Obtains the value of the first key-value pair based on the specified key.

If the key-value pair does not exist, undefined is returned.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Utils.Lang

Parameters

Name Type Mandatory Description
name string Yes Specifies the key name.

Return value

Type Description
string | null The first value is returned. If no value is found, null is returned.

Example

let paramsObject = new url.URLParams('name=Jonathan&age=18');
let name = paramsObject.get('name'); // is the string "Jonathan"
let age = paramsObject.get('age'); // is the string "18"
let absentValue = paramsObject.get('abc'); // undefined

[h2]has9+

has(name: string): boolean

Checks whether a key has a value.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Utils.Lang

Parameters

Name Type Mandatory Description
name string Yes Key specified to search for its value.

Return value

Type Description
boolean Check result. The value true is returned if the value exists; otherwise, false is returned.

Example

// Parse the URL string.
let urlObject = url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2');
// Construct a URLParams object.
let paramsObject = new url.URLParams(urlObject.search.slice(1));
// Determine whether the key name "bard" exists.
let result = paramsObject.has('bard');

[h2]set9+

set(name: string, value: string): void

Sets the value associated with name in the URLParams object to value.

If a key-value pair with the name name exists, set the value of the first key-value pair to value and delete all other values. If the key name does not exist, append the key-value pair to the query string.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Utils.Lang

Parameters

Name Type Mandatory Description
name string Yes Key name of the parameter to be set.
value string Yes Value to set.

Example

let urlObject = url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsObject = new url.URLParams(urlObject.search.slice(1));
paramsObject.set('baz', '3'); // Add a third parameter.

[h2]sort9+

sort(): void

Sorts all key-value pairs contained in this object, applicable to URL normalization scenarios (such as URL signing, cache key generation, and other scenarios that require consistent parameter order). The sort order is based on the Unicode code points of the keys. This method uses a stable sorting algorithm (preserving the relative order of key-value pairs with equal keys).

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Utils.Lang

Example

let paramsObject = new url.URLParams("c=3&a=9&b=4&d=2"); // Create a test URLParams object
paramsObject.sort(); // Sort the key/value pairs
console.info(paramsObject.toString()); // Display the sorted query string // Output a=9&b=4&c=3&d=2

[h2]keys9+

keys(): IterableIterator<string>

Returns an iterator containing the name of all key-value pairs.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Utils.Lang

Return value

Type Description
IterableIterator<string> Returns an iterator containing the name of all key-value pairs.

Example

// Construct a URLParams object.
let paramsObject = new url.URLParams("key1=value1&key2=value2");
// Get the iterator of all key names.
let keys = paramsObject.keys();
// Iterate and output the key names.
for (let key of keys) {
  console.info(key);
}
// key1
// key2

[h2]values9+

values(): IterableIterator<string>

Returns an iterator containing the value of all key-value pairs.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Utils.Lang

Return value

Type Description
IterableIterator<string> Returns an iterator containing the value of all key-value pairs.

Example

// Construct a URLParams object.
let paramsObject = new url.URLParams("key1=value1&key2=value2");
// Get the iterator of all values.
let values = paramsObject.values();
// Traverse and output the values.
for (let value of values) {
  console.info(value);
}
// value1
// value2

[h2][Symbol.iterator]9+

[Symbol.iterator](): IterableIterator<[string, string]>

Returns an iterator, where each item of the iterator is an Array. The first item of the Array is name, and the second item of the Array is value.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Utils.Lang

Return value

Type Description
IterableIterator<[string, string]> Returns an iterator, where each item is a [string, string] array containing name and value.

Example

// Construct a URLParams object.
const paramsObject = new url.URLParams('fod=bay&edg=bap');
// Get the Symbol.iterator iterator.
let iter = paramsObject[Symbol.iterator]();
// Iterate over key-value pairs.
for (let pair of iter) {
  console.info(pair[0] + ', ' + pair[1]);
}
// fod, bay
// edg, bap

[h2]toString9+

toString(): string

Obtains search parameters that are serialized as a string and, if necessary, percent-encodes the characters in the string.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Utils.Lang

Return value

Type Description
string String of serialized search parameters, which is percent-encoded if necessary.

Example

// Parse the URL string.
let urlObject = url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2');
// Construct a URLParams object.
let params = new url.URLParams(urlObject.search.slice(1));
// Append parameters.
params.append('fod', '3');
// Serialize the parameters as a string.
console.info(params.toString()); // Output 'fod=1&bard=2&fod=3'

URL

Used for parsing, constructing, normalizing, and encoding the corresponding URL string.

[h2]Properties

System capability: SystemCapability.Utils.Lang

Name Type Read-Only Optional Description
hash string No No String that contains a harsh mark (#) followed by the fragment identifier of a URL. Atomic service API: This API can be used in atomic services since API version 11.
host string No No Host information in a URL. Atomic service API: This API can be used in atomic services since API version 11.
hostname string No No Hostname (without the port) in a URL. Atomic service API: This API can be used in atomic services since API version 11.
href string No No String that contains the whole URL. Atomic service API: This API can be used in atomic services since API version 11.
origin string Yes No Read-only string that contains the Unicode serialization of the origin of the represented URL. Atomic service API: This API can be used in atomic services since API version 11.
password string No No Password in a URL. Atomic service API: This API can be used in atomic services since API version 11.
pathname string No No Path in a URL. Atomic service API: This API can be used in atomic services since API version 11.
port string No No Get and set the port part of the URL. When port is the default port of the current protocol, port will be parsed as an empty string. Atomic Service API: Since API version 11, this interface supports use in atomic services.
protocol string No No Protocol in a URL. Atomic service API: This API can be used in atomic services since API version 11.
search string No No Serialized query string in a URL. Atomic service API: This API can be used in atomic services since API version 11.
searchParams(deprecated) URLSearchParams Yes No

Obtains the URLSearchParams object, used to access URL query parameters.

- Note: This attribute is supported since API version 7 and deprecated since API version 9. It is recommended to use params9+ instead.

params9+ URLParams Yes No Obtains the URLParams object, used to access URL query parameters. Atomic Service API: Since API version 11, this interface supports use in atomic services.
username string No No Username in a URL. Atomic service API: This API can be used in atomic services since API version 11.

When parsing a URL string, if the port in the input is the default port of the current protocol, the port is parsed as an empty string. The default ports are:

  • http: 80
  • https: 443
  • ftp: 21
  • gopher: 70
  • ws: 80
  • wss: 443

Example

let parsedUrl = url.URL.parseURL('http://username:password@host:8080/directory/file?foo=1&bar=2#fragment');
console.info("hash " + parsedUrl.hash); // hash #fragment
console.info("host " + parsedUrl.host); // host host:8080
console.info("hostname " + parsedUrl.hostname); // hostname host
console.info("href " + parsedUrl.href); // href http://username:password@host:8080/directory/file?foo=1&bar=2#fragment
console.info("origin " + parsedUrl.origin); // origin http://host:8080
console.info("password " + parsedUrl.password); // password password
console.info("pathname " + parsedUrl.pathname); // pathname /directory/file
console.info("port " + parsedUrl.port); // port 8080
console.info("protocol " + parsedUrl.protocol); // protocol http:
console.info("search " + parsedUrl.search); // search ?foo=1&bar=2
console.info("username " + parsedUrl.username); // username username
// The return value of parsedUrl.params is a URLParams object.
console.info("params: foo " + parsedUrl.params.get("foo")); // params: foo 1

let urlObj = url.URL.parseURL('http://testhost:80/directory/file?foo=1');
console.info("port " + urlObj.port); // port
console.info("toString " + urlObj.toString()); // toString http://testhost/directory/file?foo=1

[h2]constructor(deprecated)

constructor(url: string, base?: string | URL)

Constructor of URL. It has the same functionality as the parseURL method, but parseURL is a static factory method. You are advised to use parseURL to create a URL object.

This API is supported since API version 7 and deprecated since API version 9. You are advised to use parseURL9+ instead.

System capability: SystemCapability.Utils.Lang

Parameters

Name Type Mandatory Description
url string Yes

A string representing an absolute URL or a relative URL. It must be in a valid URL format.

If url is a relative URL, base must be specified to resolve the final URL.

If url is an absolute URL, the given base will not take effect.

base string | URL No

Input parameter string or object. The default value is undefined.

- string: a string representing the base URL. It must be a valid URL format when url is a relative URL.

- URL: a parsed URL object, used as the base address for parsing a relative URL.

Example

let baseUrl = 'https://username:password@host:8080';
let rootPathUrl = new url.URL("/", baseUrl); // Output 'https://username:password@host:8080/';
let absoluteUrl = new url.URL(baseUrl); // Output 'https://username:password@host:8080/';
new url.URL('path/path1', absoluteUrl); // Output 'https://username:password@host:8080/path/path1';
let relativePathUrl = new url.URL('/path/path1', absoluteUrl);  // Output 'https://username:password@host:8080/path/path1';
new url.URL('/path/path1', relativePathUrl); // Output 'https://username:password@host:8080/path/path1';
new url.URL('/path/path1', rootPathUrl); // Output 'https://username:password@host:8080/path/path1';
new url.URL('/path/path1', "https://www.exampleUrl/fr-FR/toot"); // Output https://www.exampleUrl/path/path1
new url.URL('/path/path1', ''); // Raises a TypeError exception as '' is not a valid URL
new url.URL('/path/path1'); // Raises a TypeError exception as '/path/path1' is not a valid URL
new url.URL('https://www.example.com', ); // Output https://www.example.com/
new url.URL('https://www.example.com', absoluteUrl); // Output https://www.example.com/

[h2]constructor9+

constructor()

Parameterless constructor of URL. It is not recommended to call it directly. Use the parseURL method to create a URL object.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Utils.Lang

[h2]parseURL9+

static parseURL(url: string, base?: string | URL): URL

Parses a URL string and returns the parsed URL object. The object contains URL components such as the protocol, host, port, path, and query parameters.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Utils.Lang

Parameters

Name Type Mandatory Description
url string Yes

A string representing an absolute URL or a relative URL.

If url is a relative URL, base must be specified to resolve the final URL.

If url is an absolute URL, the given base will not take effect.

base string | URL No

Input parameter string or object. The default value is undefined.

- string: String. When the first parameter is a relative URL, this parameter needs to comply with the URL standard.

- URL: URL object.

- Used when url is a relative URL. This parameter will not take effect when url is an absolute URL.

Return value

Type Description
URL Returns the parsed URL object, containing the components of the URL (such as protocol, host, and path attributes).

If url is a relative URL, the URL parsed by calling this API is not merely a concatenation of url and base. When url is a relative path, it is parsed relative to the current directory of the passed base, taking into account all path segments before the last slash in the path of base, but excluding the following part (see example url1). When url points to a root directory, it is parsed relative to the origin of base (see example url2).

Error codes

For details about the error codes, see Utils Error Codes.

ID Error Message
10200002 Invalid url string.

Example

let baseUrl = 'https://username:password@host:8080/test/test1/test3';
let urlObject = url.URL.parseURL(baseUrl);
let result = urlObject.toString(); // Output 'https://username:password@host:8080/test/test1/test3'
// If url is a relative path, the path in the base parameter is test/test1, and the path of the parsed URL is /test/path2/path3.
let relativePathUrl = url.URL.parseURL('path2/path3', 'https://www.example.com/test/test1'); // Output 'https://www.example.com/test/path2/path3'
// If url is a root directory, the path in the base parameter is /test/test1/test3, and the path of the parsed URL is /path1/path2.
let rootPathUrl = url.URL.parseURL('/path1/path2', urlObject); // Output 'https://username:password@host:8080/path1/path2'
url.URL.parseURL('/path/path1', "https://www.exampleUrl/fr-FR/toot"); // Output 'https://www.exampleUrl/path/path1'
url.URL.parseURL('/path/path1', ''); // Raises a TypeError exception as '' is not a valid URL
url.URL.parseURL('/path/path1'); // Raises a TypeError exception as '/path/path1' is not a valid URL
url.URL.parseURL('https://www.example.com', ); // Output 'https://www.example.com/'
url.URL.parseURL('https://www.example.com', urlObject); // Output 'https://www.example.com/'

[h2]toString

toString(): string

Converts the parsed URL to a string. The return value is the same as the value of the href attribute of the URL.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Utils.Lang

Return value

Type Description
string Serialized string of the parsed URL.

Example

const urlObject = url.URL.parseURL('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
let result = urlObject.toString(); // Output 'https://username:password@host:8080/directory/file?query=pppppp#qwer=da'

[h2]toJSON

toJSON(): string

Converts the parsed URL into a JSON string.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Utils.Lang

Return value

Type Description
string JSON serialized string of the URL object.

Example

// Parse the URL string.
const urlObject = url.URL.parseURL('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
// Convert the URL to a string.
let result = urlObject.toJSON();

URLSearchParams(deprecated)

The URLSearchParams interface defines some utility methods for processing URL query strings.

This API is supported since API version 7 and deprecated since API version 9. You are advised to use URLParams instead.

[h2]constructor(deprecated)

constructor(init?: string[][] | Record<string, string> | string | URLSearchParams)

A constructor used to create a URLSearchParams instance.

This API is supported since API version 7 and deprecated since API version 9. You are advised to use URLParams.constructor9+ instead.

System capability: SystemCapability.Utils.Lang

Parameters

Name Type Mandatory Description
init string[][] | Record<string, string> | string | URLSearchParams No

Input parameter object.

- string[][]: two-dimensional string array, each inner array contains two elements, which are the key name and key value respectively.

- Record<string, string>: object list.

- string: String, which must follow the URL query parameter format, such as 'key=value&key2=value2'.

- URLSearchParams: Object.

- Default value: undefined.

Example

let objectParams = new url.URLSearchParams([ ['user1', 'abc1'], ['query2', 'first2'], ['query3', 'second3'] ]);
let objectParams1 = new url.URLSearchParams({"fod" : '1' , "bard" : '2'});
let objectParams2 = new url.URLSearchParams('?fod=1&bard=2');
let urlObject = new url.URL('https://developer.mozilla.org/?fod=1&bard=2');
let params = new url.URLSearchParams(urlObject.search);

[h2]append(deprecated)

append(name: string, value: string): void

Appends a key-value pair into the query string.

This API is supported since API version 7 and deprecated since API version 9. You are advised to use URLParams.append9+ instead.

System capability: SystemCapability.Utils.Lang

Parameters

Name Type Mandatory Description
name string Yes Key of the key-value pair to append.
value string Yes Value of the key-value pair to append.

Example

let urlObject = new url.URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsObject = new url.URLSearchParams(urlObject.search.slice(1));
paramsObject.append('fod', '3');

[h2]delete(deprecated)

delete(name: string): void

Deletes key-value pairs of the specified key.

This API is supported since API version 7 and deprecated since API version 9. You are advised to use URLParams.delete9+ instead.

System capability: SystemCapability.Utils.Lang

Parameters

Name Type Mandatory Description
name string Yes Key of the key-value pairs to delete.

Example

let urlObject = new url.URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsObject = new url.URLSearchParams(urlObject.search.slice(1));
paramsObject.delete('fod');

[h2]getAll(deprecated)

getAll(name: string): string[]

Obtains the set of all values corresponding to the specified name.

This API is supported since API version 7 and deprecated since API version 9. You are advised to use URLParams.getAll9+ instead.

System capability: SystemCapability.Utils.Lang

Parameters

Name Type Mandatory Description
name string Yes Target key.

Return value

Type Description
string[] All the values obtained.

Example

let urlObject = new url.URL('https://developer.exampleUrl/?fod=1&bard=2');
let params = new url.URLSearchParams(urlObject.search.slice(1));
params.append('fod', '3'); // Add a second value for the fod parameter.
console.info(params.getAll('fod').toString()) // Output ["1","3"].

[h2]entries(deprecated)

entries(): IterableIterator<[string, string]>

Returns an iterator, where each item of the iterator is an Array. The first item of the Array is name, and the second item of the Array is value.

This API is supported since API version 7 and deprecated since API version 9. You are advised to use URLParams.entries9+ instead.

System capability: SystemCapability.Utils.Lang

Return value

Type Description
IterableIterator<[string, string]> Returns an iterator, where each item is a [string, string] array containing name and value.

Example

let searchParamsObject = new url.URLSearchParams("keyName1=valueName1&keyName2=valueName2");
let iter = searchParamsObject.entries();
for (let pair of iter) {
  console.info(pair[0]+ ', '+ pair[1]);
}
// keyName1, valueName1
// keyName2, valueName2

[h2]forEach(deprecated)

forEach(callbackFn: (value: string, key: string, searchParams: URLSearchParams) => void, thisArg?: Object): void

Traverses the key-value pairs in the URLSearchParams instance by using a callback.

This API is supported since API version 7 and deprecated since API version 9. You are advised to use URLParams.forEach9+ instead.

System capability: SystemCapability.Utils.Lang

Parameters

Name Type Mandatory Description
callbackFn function Yes Callback invoked to traverse the key-value pairs in the URLSearchParams instance.
thisArg Object No Value of this to use when callbackFn is invoked. The default value is this object.

Table 1 callbackFn parameter description

Name Type Mandatory Description
value string Yes Value that is currently traversed.
key string Yes Key that is currently traversed.
searchParams URLSearchParams Yes Instance that invokes the forEach method.

Example

const myURLObject = new url.URL('https://developer.exampleUrl/?fod=1&bard=2');
myURLObject.searchParams.forEach((value, name, searchParams) => {
    console.info(name, value, myURLObject.searchParams === searchParams);
});

[h2]get(deprecated)

get(name: string): string | null

Obtains the value of the first key-value pair based on the specified key.

If a nonexistent key-value pair name is searched for, the return value is undefined. This API is supported since API version 7 and deprecated since API version 9. You are advised to use URLParams.get9+ instead.

System capability: SystemCapability.Utils.Lang

Parameters

Name Type Mandatory Description
name string Yes Key specified to obtain the value.

Return value

Type Description
string | null The first value is returned. If no value is found, null is returned.

Example

let paramsObject = new url.URLSearchParams('name=Jonathan&age=18');
let name = paramsObject.get("name"); // is the string "Jonathan"
let age = paramsObject.get("age"); // is the string '18'
let getObj = paramsObject.get("abc"); // undefined

[h2]has(deprecated)

has(name: string): boolean

Checks whether a key has a value.

This API is supported since API version 7 and deprecated since API version 9. You are advised to use URLParams.has9+ instead.

System capability: SystemCapability.Utils.Lang

Parameters

Name Type Mandatory Description
name string Yes Key specified to search for its value.

Return value

Type Description
boolean Check result. The value true is returned if the value exists; otherwise, false is returned.

Example

let urlObject = new url.URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsObject = new url.URLSearchParams(urlObject.search.slice(1));
paramsObject.has('bard') === true;

[h2]set(deprecated)

set(name: string, value: string): void

Sets the value for a key. If key-value pairs matching the specified key exist, the value of the first key-value pair will be set to the specified value and other key-value pairs will be deleted. Otherwise, the key-value pair will be appended to the query string.

This API is supported since API version 7 and deprecated since API version 9. You are advised to use URLParams.set9+ instead.

System capability: SystemCapability.Utils.Lang

Parameters

Name Type Mandatory Description
name string Yes Key of the value to set.
value string Yes Value to set.

Example

let urlObject = new url.URL('https://developer.exampleUrl/?fod=1&bard=2');
let paramsObject = new url.URLSearchParams(urlObject.search.slice(1));
paramsObject.set('baz', '3'); // Add a third parameter.

[h2]sort(deprecated)

sort(): void

Sorts all key-value pairs contained in this object. The sort order is based on the Unicode code points of the keys. This method uses a stable sorting algorithm (that is, the relative order between key-value pairs with equal keys will be preserved).

This API is supported since API version 7 and deprecated since API version 9. You are advised to use URLParams.sort9+ instead.

System capability: SystemCapability.Utils.Lang

Example

let searchParamsObject = new url.URLSearchParams("c=3&a=9&b=4&d=2"); // Create a test URLSearchParams object
searchParamsObject.sort(); // Sort the key/value pairs
console.info(searchParamsObject.toString()); // Display the sorted query string // Output a=9&b=4&c=3&d=2

[h2]keys(deprecated)

keys(): IterableIterator<string>

Returns an iterator of the names of all key-value pairs.

This API is supported since API version 7 and deprecated since API version 9. You are advised to use URLParams.keys9+ instead.

System capability: SystemCapability.Utils.Lang

Return value

Type Description
IterableIterator<string> Returns an iterator of the names of all key-value pairs.

Example

let searchParamsObject = new url.URLSearchParams("key1=value1&key2=value2");
let keys = searchParamsObject.keys();
for (let key of keys) {
  console.info(key);
}
// key1
// key2

[h2]values(deprecated)

values(): IterableIterator<string>

Returns an iterator of the values of all key-value pairs.

This API is supported since API version 7 and deprecated since API version 9. You are advised to use URLParams.values9+ instead.

System capability: SystemCapability.Utils.Lang

Return value

Type Description
IterableIterator<string> Returns an iterator of the values of all key-value pairs.

Example

let searchParams = new url.URLSearchParams("key1=value1&key2=value2");
let values = searchParams.values();
for (let value of values) {
  console.info(value);
}
// value1
// value2

[h2][Symbol.iterator](deprecated)

[Symbol.iterator](): IterableIterator<[string, string]>

Returns an iterator, where each item of the iterator is an Array. The first item of the Array is name, and the second item of the Array is value.

This API is supported since API version 7 and deprecated since API version 9. You are advised to use URLParams.[Symbol.iterator]9+ instead.

System capability: SystemCapability.Utils.Lang

Return value

Type Description
IterableIterator<[string, string]> Returns an iterator, where each item is a [string, string] array containing name and value.

Example

const paramsObject = new url.URLSearchParams('fod=bay&edg=bap');
let pairs = paramsObject[Symbol.iterator]();
for (let pair of pairs) {
  console.info(pair[0] + ', ' + pair[1]);
}
// fod, bay
// edg, bap

[h2]toString(deprecated)

toString(): string

Obtains search parameters that are serialized as a string and, if necessary, percent-encodes the characters in the string.

This API is supported since API version 7 and deprecated since API version 9. You are advised to use URLParams.toString9+ instead.

System capability: SystemCapability.Utils.Lang

Return value

Type Description
string String of serialized search parameters, which is percent-encoded if necessary.

Example

let urlObject = new url.URL('https://developer.exampleUrl/?fod=1&bard=2');
let params = new url.URLSearchParams(urlObject.search.slice(1));
params.append('fod', '3');
console.info(params.toString()); // Output 'fod=1&bard=2&fod=3'