Open In App

Node.js URL.hash API

Last Updated : 14 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The url.hash is an inbuilt application programming interface of class URL within url module which is used to get and set the fragment portion of the URL.

Syntax: 

url.hash

Return value: It gets and sets the fragment portion of the URL.

Below programs illustrate the use of url.hash Method:

Example 1:  

Javascript




// node program to demonstrate the 
// url.hash API as Setter 
  
// creating and initializing myURL
const myURL = new URL('https://example.org/foo#ram');
  
// Display href value of myURL before change
console.log("Before Change");
console.log(myURL.href);
  
// assigning fragment portion
// using hash 
console.log();
myURL.hash = 'rahim';
  
// Display href value of myURL after change
console.log("After Change");
console.log(myURL.href);


Output

Before Change
https://example.org/foo#ram

After Change
https://example.org/foo#rahim

Example 2:  

Javascript




// node program to demonstrate the 
// url.hash API as Getter 
  
// creating and initializing myURL
const myURL = new URL('https://example.org/foo#ram');
  
// getting the fragment portion
// using hash 
const hash = myURL.hash;
  
// Display hash value 
console.log(hash);


Output: 

#ram

NOTE: The above program could be run by using the node myapp.js command. 

Reference: 
https://nodejs.org/dist/latest-v10.x/docs/api/url.html#url_url_hash
 



Previous Article
Next Article

Similar Reads

Node.js urlObject.hash API
Before we go on to learn about URL objects API we need to know a brief about URL. If you ever wanted to provide utilities so as to parse or resolute your URL, we can do it through the URL Module. We can divide the URL Module into two parts:- These are- URL String and URL Object. The urlObjectHash is a part of URL Object which is generally based on
1 min read
How to read a hash with an “&” sign in the URL ?
It's a very common situation in web development where users want to extract or read information from any URL. In the case of the unavailability of the server-side code, the programmer can make use of JavaScript or jQuery to read information. To read the URL with an & sign, we can use the split() function. By that function, we can easily get the
2 min read
How to check a URL contains a hash or not using JavaScript ?
In this article, the task is to check whether an URL contains or not. This can be done by using the Location hash property in JavaScript. It returns the string which represents the anchor part of a URL including the hash ‘#’ sign. Syntax: window.location.hash Example: This example uses the hash property to check if the URL contains hash or not usin
1 min read
Node.js URL.format(urlObject) API
The URL.format(urlObject) is the inbuilt API provided by URL class, which takes an object or string and return a formatted string derived from that object or string. Syntax: const url.format(urlObject) If the urlObject is not an object or string, then it will throw a TypeError. Return value: It returns string derived from urlObject. The urlObject c
4 min read
Node.js URL.host API
The url.host is an inbuilt application programming interface of class URL with in url module which is used to get and set the host portion of the URL. Syntax: const url.host Return value: It gets and sets the host portion of the URL.Below programs illustrates the use of url.host Method:Example 1: C/C++ Code // node program to demonstrate the // url
1 min read
Node.js URL.hostname API
The url.hostname is an inbuilt application programming interface of class URL with in url module which is used to get and set the hostname portion of the URL. The key difference between url.host and url.hostname is that url.hostname does not include the port. Syntax: const url.hostname Return value: It gets and sets the hostname portion of the URL.
1 min read
Node.js URL.href API
The url.href is an inbuilt application programming interface of class URL with in the url module which Gets and sets the serialized URL. Getting the value of the href property is equivalent to calling the url.toString() method.Setting the value of this property to a new value is equivalent to creating a new URL object using new URL(value). Each of
2 min read
Node.js URL.password API
The url.password is an inbuilt application programming interface of class URL with in url module which is used to get and set the password portion of the URL. Syntax: const url.password Return value: It gets and sets the password portion of the URL.Below programs illustrate the use of url.password Method:Example 1: C/C++ Code // node program to dem
2 min read
Node.js URL.origin API
url.origin is an inbuilt application programming interface(API) of the URL class within the url module. url.origin API is used to gets the read-only serialization of the URL’s origin. Syntax: url.origin url : It is an object created by URL constructor. Example 1: C/C++ Code //Importing the url module const url = require('url'); //Creating an URL_1
1 min read
Node.js URL.username API
URL.username is an inbuilt application programming interface(API) of the URL class within Node.JS. URL.username API is used to get and set the username of the URL. Syntax: url.username URL: It is an object created by a URL constructor. Example 1: (Getting the username of URL) C/C++ Code //Creating an URL_1 object with URL constructor. const URL_1 =
1 min read