• Node.js Video Tutorials

NodeJS - url.hash 屬性



URL 類別的 NodeJS url.hash 屬性 用於獲取和設定 URL 的片段部分。

分配給 NodeJS url.hash 屬性 的值包括經過百分比編碼的無效 URL 字元。用於百分比編碼的字元可能與 URL.parse() 和 URL.format() 方法返回的字元略有不同。

語法

以下是 URL 類別的 NodeJS hash 屬性 的語法

URL.hash

引數

此屬性不接受任何引數。

返回值

此屬性獲取並設定提供的 URL 的片段部分。

示例

我們可以使用 URL.hash 屬性獲取 URL 的片段部分的值。

在下面的示例中,我們嘗試獲取提供的 URL 的片段部分。

const url = require('url');

const myURL = new URL('https://tutorialspoint.tw/index.htm#HYD');
console.log(myURL.href);
console.log("Fragment portion value: " + myURL.hash);

輸出

執行上述程式後,myURL.hash 屬性返回 URL 片段部分中的值。

https://tutorialspoint.tw/index.htm#HYD
Fragment portion value: #HYD

示例

我們還可以使用 URL.hash 屬性設定 URL 的片段部分的值。

在下面的程式中,我們嘗試為給定 URL 的片段部分設定一個新值。

const url = require('url');

const myURL = new URL('https://tutorialspoint.tw/index.htm#HYD');
console.log(myURL.href);
console.log("The current fragment portion in URL: " + myURL.hash);


myURL.hash = "Delhi";
console.log("After modifying the fragment portion to: " + myURL.hash);
console.log(myURL.href);

輸出

如我們在下面的輸出中看到的,myURL.hash 屬性修改了給定 URL 的片段部分。

https://tutorialspoint.tw/index.htm#HYD
The current fragment portion in URL: #HYD

After modifying the fragment portion to: #Delhi
https://tutorialspoint.tw/index.htm#Delhi
nodejs_url_module.htm
廣告