从组成部分构造网址并获取构造的字符串


可以使用属性设置器或模板文字串从组件部分构建 WHATWG 网址:

const myURL = new URL('https://example.org');
myURL.pathname = '/a/b/c';
myURL.search = '?d=e';
myURL.hash = '#fgh';
const pathname = '/a/b/c';
const search = '?d=e';
const hash = '#fgh';
const myURL = new URL(`https://example.org${pathname}${search}${hash}`);

要获取构造的网址字符串,则使用 href 属性访问器:

console.log(myURL.href);

It is possible to construct a WHATWG URL from component parts using either the property setters or a template literal string:

const myURL = new URL('https://example.org');
myURL.pathname = '/a/b/c';
myURL.search = '?d=e';
myURL.hash = '#fgh';
const pathname = '/a/b/c';
const search = '?d=e';
const hash = '#fgh';
const myURL = new URL(`https://example.org${pathname}${search}${hash}`);

To get the constructed URL string, use the href property accessor:

console.log(myURL.href);