📜  Node.js URL.hash API(1)

📅  最后修改于: 2023-12-03 15:33:10.116000             🧑  作者: Mango

Node.js URL.hash API

Node.js URL.hash API is a built-in module that provides various methods for working with the hash portion of a URL.

Getting started

To use the Node.js URL.hash API, first, you need to import the 'url' module using the require() method, like so:

const url = require('url');

Once you have imported the 'url' module, you can use the various methods provided by the URL class to work with URLs.

Methods provided by URL.hash

The URL.hash object provides the following methods:

url.hash

The url.hash property returns the hash portion of the URL, including the hash sign. If the URL does not have a hash, this property will return an empty string.

const myUrl = new URL('https://example.com/#foo');
console.log(myUrl.hash);
// Output: "#foo"
url.hash.substring(start [, end])

The url.hash.substring() method returns a part of the hash. You can specify the start and end indexes to get a specific portion of the hash.

const myUrl = new URL('https://example.com/#foo');
console.log(myUrl.hash.substring(1));
// Output: "foo"
url.hash.substr(start [, length])

The url.hash.substr() method returns a specified part of the hash. You can specify the start index and the length of the hash to get a specific portion of the hash.

const myUrl = new URL('https://example.com/#foo');
console.log(myUrl.hash.substr(1, 2));
// Output: "fo"
Conclusion

That's it! These are the methods provided by Node.js URL.hash API. You can use these methods to work with the hash portion of a URL in your Node.js applications.