Skip to content
Related Articles

Related Articles

How to check a URL contains a hash or not using JavaScript ?
  • Last Updated : 10 Dec, 2020

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:

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body style="text-align: center">
  
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
  
    <h2 style="font-family: Impact;">
        How to check if URL contains an 
        hash or not using Javascript?
    </h2>
  
    <p>
        please double click the button:
    </p>
  
    <button ondblclick="mylocation()">
        check
    </button>
  
    <p id="hash"></p>
  
    <script>
        function mylocation() {
            if (window.location.hash) {
                alert("Hash(#) component exists");
            }
            else {
                alert("Hash(#) component doesn't exist");
            }
        
    </script>
</body>
  
</html>

chevron_right


Output:

Before Clicking On Button:



After Clicking On Button:

Supported Browsers:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Safari
  • Opera

full-stack-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :