• Node.js Video Tutorials

NodeJS - urlObject.href 屬性



NodeJS urlObject.href 屬性 指定了已解析的完整小寫 URL 字串,包含協議和主機部分。如果 URL 字串包含大寫字母,href 屬性會將其轉換為小寫。

例如,對於 URL “https://user:pass@SITE.com/pa/th?=val#hash”,hash 屬性的返回值將是 “https://user:pass@Site.com/pa/th?=val#hash”。

語法

以下是 NodeJS urlObject.href 屬性 的語法

urlObject.href

引數

此屬性不接受任何引數。

返回值

此屬性檢索已解析的完整 URL 字串,包括已轉換為小寫的協議和主機部分。

示例

以下示例演示了 NodeJS urlObject.href 屬性的用法。

const url = require('url');
let address = 'https://user:pass@tutorialspoint.com/pa/th?=val#hashh';
let result = url.parse(address, true);
console.log(result.href);

輸出

從下面的輸出中可以看到,NodeJS href 屬性檢索了完整的 URL 字串,包括協議和主機部分。

https://user:pass@tutorialspoint.com/pa/th?=val#hashh

示例

如果我們不解析指定的 URL,auth 屬性將為 undefined。

我們嘗試在不解析的情況下使用 href 屬性獲取完整的 URL 字串。

const url = require('url');
let address = 'https://user:pass@tutorialspoint.com/pa/th?=val#hashh';
console.log(address.href);

輸出

從下面的輸出中可以看到,href 屬性為 undefined。

undefined
nodejs_url_module.htm
廣告
© . All rights reserved.