tencent cloud

All product documents
Tencent Cloud Observability Platform
URL Overview
Last updated: 2025-03-11 20:21:20
URL Overview
Last updated: 2025-03-11 20:21:20
url.URL can be used for URL-related operations.

Constructor

Create an object instance using new, as shown below:
new URL(url: string, base?: string | URL): URL

Parameters

Parameter
Type
Description
url
string
The Uniform Resource Locator.
base?
string or URL
The protocol and host portions of the Uniform Resource Locator, which will be overridden if not included in the URL.

Methodology

Methodology
Return Type
Description
hash()
string
Get the fragment portion of the URL.
void
Set the fragment portion of the URL.
host()
string
Get the host portion of the URL.
void
Set the host portion of the URL.
string
Get the host name portion of the URL.
void
Set the host name portion of the URL.
string
Get the serialized URL.
void
Set the serialized URL.
origin()
string
Obtain the read-only serialized string of the origin of the URL.
string
Get the password portion of the URL.
void
Set the password portion of the URL.
string
Get the path portion of the URL.
void
Set the path portion of the URL.
port()
string
Get the port portion of the URL.
void
Set the port portion of the URL.
string
Get the protocol portion of the URL.
void
Set the protocol portion of the URL.
search()
string
Get the serialized query portion of the URL.
void
Set the serialized query portion of the URL.
Get the URLSearchParams object representing the URL query parameters.
string
Get the username portion of the URL.
void
Set the username portion of the URL.
toJSON()
string
Return the serialized URL, this method is automatically called when the URL object is serialized with JSON.stringify().
string
Return the serialized URL.

Samples

Create a URL object without using the base parameter:
import url from 'pts/url';

export default function () {
const u = 'http://user:pass@www.example.com:8080/test/index.html?name=xxx&age=18#worker';
const u0 = new url.URL(u)
console.log(u0.toString()); // http://user:pass@www.example.com:8080/test/index.html?name=xxx&age=18#worker
}
Create a URL object using the base parameter:
import url from 'pts/url';

export default function () {
const u = '/test/index.html?name=xxx&age=18#worker';
const u0 = new url.URL(u, 'https://console.cloud.tencent.com:8080')
console.log(u0.toString()); // https://console.cloud.tencent.com:8080/test/index.html?name=xxx&age=18#worker
}
Perform related operations using the URL object:
import url from 'pts/url';

export default function () {
const u = 'http://user:pass@www.example.com:8080/test/index.html?name=xxx&age=18#worker';

const u1 = new url.URL(u);
console.log('hash1 ', u1.hash()); // #worker
u1.setHash('hash');
console.log('hash2 ', u1.hash()); // #hash

const u2 = new url.URL(u);
console.log('host1 ', u2.host()); // www.example.com:8080
u2.setHost('host');
console.log('host2 ', u2.host()); // host

const u3 = new url.URL(u);
console.log('hostname1 ', u3.hostname()); // www.example.com
u3.setHostname('hostname');
console.log('hostname2 ', u3.hostname()); // hostname

const u4 = new url.URL(u);
console.log('href1 ', u4.href()); // http://user:pass@www.example.com:8080/test/index.html?name=xxx&age=18#worker
u4.setHref('https://console.cloud.tencent.com');
console.log('href2 ', u4.href()); // https://console.cloud.tencent.com/

const u5 = new url.URL(u);
console.log('origin1 ', u5.origin()); // http://www.example.com:8080

const u6 = new url.URL(u);
console.log('pathname1 ', u6.pathname()); // /test/index.html
u6.setPathname('pathname');
console.log('pathname2 ', u6.pathname()); // pathname

const u7 = new url.URL(u);
console.log('port1 ', u7.port()); // 8080
u7.setPort('80');
console.log('port2 ', u7.port()); // 80

const u8 = new url.URL(u);
console.log('protocol1 ', u8.protocol()); // http:
u8.setProtocol('protocol');
console.log('protocol2 ', u8.protocol()); // protocol:

const u9 = new url.URL(u);
console.log('search1 ', u9.search()); // ?age=18&name=xxx
u9.setSearch('search');
console.log('search2 ', u9.search()); // ?search=

const u10 = new url.URL(u);
console.log('searchParams1 ', u10.searchParams()); // [object Object]

const u11 = new url.URL(u);
console.log('username1 ', u11.username()); // user
u11.setUsername('username');
console.log('username2 ', u11.username()); // username

const u12 = new url.URL(u);
console.log('password1 ', u12.password()); // pass
u12.setPassword('password');
console.log('password2 ', u12.password()); // password

const u13 = new url.URL(u);
console.log('toJSON1 ', u13.toJSON()); // http://user:pass@www.example.com:8080/test/index.html?name=xxx&age=18#worker

const u14 = new url.URL(u);
console.log('toString1 ', u14.toString()); // http://user:pass@www.example.com:8080/test/index.html?name=xxx&age=18#worker
}

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

7x24 Phone Support