Thursday, September 4, 2025
HomeLanguagesJavascriptHow to check a URL contains a hash or not using JavaScript...

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 using javascript.

HTML




<body style="text-align: center">
    <h1 style="color: green;">
        neveropen
    </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>


Output:

 Check a URL contains a hash or not

 Check a URL contains a hash or not

RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6628 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11858 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS