tencent cloud

云压测

产品概述
购买指南
计费概述
按量计费(后付费)
购买方式
欠费说明
退费说明
快速入门
操作指南
简单模式压测
脚本模式压测
JMeter 模式压测
管理项目
管理场景
流量录制
环境管理
定时压测
压测报告
访问控制
告警管理
标签管理
错误代码手册
实践教程
使用 Prometheus 观测性能压测指标
使用云压测回放 GoReplay 录制的请求
API 文档
History
Introduction
API Category
Making API Requests
PTS-related APIs
Data Types
Error Codes
JavaScript API 列表
JavaScript API 列表概述
pts/global
pts/http
pts
pts/dataset
pts/grpc
pts/jsonpath
pts/protobuf
pts/redis
pts/sql
pts/url
pts/util
pts/ws
pts/socketio
pts/socket
常见问题
相关协议
服务等级协议
使用限制
隐私政策
数据处理和安全协议

URL 概览

PDF
Focus Mode
Font Size
Last updated: 2025-03-10 17:23:41
url.URL 能够用于 URL 的相关操作。

构造函数

通过 new 进行对象实例创建,如下所示:
new URL(url: string, base?: string | URL): URL

参数

参数
类型
描述
url
string
统一资源定位符
base?
string 或 URL
统一资源定位符中的协议和主机部分,若 url 中不包含,则对其进行覆盖

方法

方法
返回类型
描述
hash()
string
获取网址的片段部分
void
设置网址的片段部分
host()
string
获取网址的主机部分
void
设置网址的部分
string
获取网址的主机名部分
void
设置网址的主机名部分
href()
string
获取序列化的网址
void
设置序列化的网址
origin()
string
获取网址的源的只读的序列化
string
获取网址的密码部分
void
设置网址的密码部分
string
获取网址的路径部分
void
设置网址的路径部分
port()
string
获取网址的端口部分
void
设置网址的端口部分
string
获取网址的协议部分
void
设置网址的协议部分
search()
string
获取网址的序列化的查询部分
void
设置网址的序列化的查询部分
获取表示网址查询参数的 URLSearchParams 对象
string
获取网址的用户名部分
void
设置网址的用户名部分
toJSON()
string
返回序列化的网址,当 URL 对象用 JSON.stringify() 序列化时,会自动调用此方法
string
返回序列化的网址

样例

创建 URL 对象,不使用 base 参数:
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
}
创建 URL 对象,使用 base 参数:
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.tencentcloud.com:8080')
console.log(u0.toString()); // https://console.tencentcloud.com:8080/test/index.html?name=xxx&age=18#worker
}
使用 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 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.tencentcloud.com');
console.log('href2 ', u4.href()); // https://console.tencentcloud.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
}


Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback