JavaScript

How to get hash value from URL using JavaScript

In this tutorial, we are going to see how to get hash value from URL using JavaScript. Sometimes elements of a web page are changed based on the hash value (#) of the current URL. This is a very efficient way to pass a value in the URL and load the web page based on that value. Using JavaScript, you can easily get the value after the hash (#) from a URL.
 

Script to get the hash value from URL using JavaScript :
if(window.location.hash) {
    var hash = window.location.hash.substring(1);
    alert(hash);
} else {
    // No hash is found
}

Suppose the current URL is – https://stackhowto.com/page.html#Cours
 

 

Get the value with the hash (#):
var hash = location.hash;   //#Cours

 

Get the value without the hash (#):
var hash = location.hash.substr(1);    //Cours

 
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *