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


【Constructing a URL from component parts and getting the constructed string】

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

【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}`); 

要获取构建的 URL 字符串,请使用 href 属性访问器:

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

console.log(myURL.href);