Skip to content
Related Articles

Related Articles

Improve Article

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

  • Difficulty Level : Basic
  • 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. 





<!DOCTYPE html>
<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");


Before Clicking On Button:

After Clicking On Button:

Supported Browsers:

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

Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :