• Node.js Video Tutorials

NodeJS - urlObject.hash 屬性



Node.js 的 URL 模組提供了各種用於 URL 解析和解析的實用程式。

urlObject 的 NodeJS urlObject.hash 屬性指定了 URL 中的片段段,包括前導 ASCII 雜湊“#”字元。

例如,考慮此 URL“https://user:pass@www.site.com#hash”,urlObject.hash 屬性的返回值將為 #hash。

語法

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

urlObject.hash

引數

此屬性不接受任何引數。

返回值

此屬性檢索 URL 的片段段。

示例

如果指定的 URL 包含片段段,則 hash 屬性將檢索該段。

在以下示例中,我們嘗試從指定的 URL 獲取片段段。

注意:要使用 NodeJS urlObject.hash 屬性從 URL 獲取片段段,我們首先需要使用 url.parse() 方法解析 URL;否則 hash 段將未定義。

const url = require('url');
let address = 'https://user:pass@www.Tutorialspoint.com#hashhhhh';
let result = url.parse(address, true);
console.log(result.hash);

輸出

從下面的輸出中我們可以看到,NodeJs hash 屬性從 URL 中檢索了片段段。

#hashhhhh

示例

如果提供的 URL 不包含片段段,則 hash 屬性將為 null。

const url = require('url');
let address = 'https://user:pass@www.Tutorialspoint.com';
let result = url.parse(address, true);
console.log(result.hash);

輸出

從下面的輸出中我們可以看到,hash 屬性為 null,因為 URL 中沒有涉及片段段。

null

示例

如果我們不解析指定的 URL,則 hash 屬性將未定義。

這裡我們嘗試在不解析的情況下從提供的 URL 獲取片段段。

const url = require('url');
let address = 'https://user:pass@www.Tutorialspoint.com#hashhhhh';
console.log(address.hash);

輸出

從下面的輸出中我們可以看到,hash 屬性未定義。

undefined
nodejs_url_module.htm
廣告