i2tutorials

Get hash value from URL using JavaScript

 

Elements of a web page are sometimes changed based on the hash value (#) of the current URL. This is a very efficient method for passing a value in the URL and then loading the web page based on that value. You can easily get the value after the hash (#) from a URL using JavaScript.

Getting hash value from URL:

The code below was written using the split method to obtain the hash value and display it as alerts.

<!DOCTYPE html>
 <html>
 <head>
   <title>Get hash value from URL</title>
   <meta name="description" content="">
   <meta n <link rel="stylesheet" href="Goto Previous page/styles.css">
  </head>
  <body>
   <p>Get hash value from URL : Gives us alert of query params </p>
   <script>
     var url = '“https://www.youtube.com/?watch?v=j3-LV3XxhVg”'
     hash = url.split('?')[1]+ url.split('?')[2];

   if (hash) {
      alert(hash)
   } else {
      alert("No Hash Value");
   }
   </script>
 </body>
 </html>

 Output:

Get hash value from URL using JavaScript

 

 

Exit mobile version