new URL(url: string, base?: string | URL): URL
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 | Return Type | Description |
string | Get the fragment portion of the URL. | |
void | Set the fragment portion of the URL. | |
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. | |
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. | |
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. | |
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. | |
string | Return the serialized URL, this method is automatically called when the URL object is serialized with JSON.stringify(). | |
string | Return the serialized URL. |
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}
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}
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()); // #workeru1.setHash('hash');console.log('hash2 ', u1.hash()); // #hashconst u2 = new url.URL(u);console.log('host1 ', u2.host()); // www.example.com:8080u2.setHost('host');console.log('host2 ', u2.host()); // hostconst u3 = new url.URL(u);console.log('hostname1 ', u3.hostname()); // www.example.comu3.setHostname('hostname');console.log('hostname2 ', u3.hostname()); // hostnameconst u4 = new url.URL(u);console.log('href1 ', u4.href()); // http://user:pass@www.example.com:8080/test/index.html?name=xxx&age=18#workeru4.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:8080const u6 = new url.URL(u);console.log('pathname1 ', u6.pathname()); // /test/index.htmlu6.setPathname('pathname');console.log('pathname2 ', u6.pathname()); // pathnameconst u7 = new url.URL(u);console.log('port1 ', u7.port()); // 8080u7.setPort('80');console.log('port2 ', u7.port()); // 80const 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=xxxu9.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()); // useru11.setUsername('username');console.log('username2 ', u11.username()); // usernameconst u12 = new url.URL(u);console.log('password1 ', u12.password()); // passu12.setPassword('password');console.log('password2 ', u12.password()); // passwordconst u13 = new url.URL(u);console.log('toJSON1 ', u13.toJSON()); // http://user:pass@www.example.com:8080/test/index.html?name=xxx&age=18#workerconst 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}