當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Node.js URL.href用法及代碼示例

url.href

獲取和設置序列化的 URL。

const myURL = new URL('https://example.org/foo');
console.log(myURL.href);
// Prints https://example.org/foo

myURL.href = 'https://example.com/bar';
console.log(myURL.href);
// Prints https://example.com/bar

獲取 href 屬性的值等同於調用 url.toString()

將此屬性的值設置為新值等效於使用 new URL(value) 創建新的 URL 對象。 URL 對象的每個屬性都將被修改。

如果分配給 href 屬性的值不是有效的 URL,則會拋出 TypeError

相關用法


注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 URL.href。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。