Skip to content
Related Articles

Related Articles

Node | URL.hash API
  • Difficulty Level : Basic
  • Last Updated : 04 Apr, 2020

The url.hash is an inbuilt application programming interface of class URL with in 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:






// 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);
  
// assinging 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:




// 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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :