Open In App
Related Articles

How to check a URL contains a hash or not using JavaScript ?

Improve Article
Save Article
Like Article

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. 



Example: This example uses the hash property to check if the URL contains hash or not using javascript.


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


 Check a URL contains a hash or not

 Check a URL contains a hash or not

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 12 Jan, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials