jQuery

jquery check for # (Hash) in URL

Ever wanted to check if # (Hash) is present in URL, using jQuery? You can do that easily with few line of code.

How to check for # (Hash) in URL using jQuery

Here is a very easy example.

<script type="text/javascript">

$(document).ready(function() {

if(window.location.hash)
{
//If # exists, do this.
}
else
{
//If # does not exist, do this.
}

});

</script>

Note:

The above code will trigger as soon as the page loads. So if you want it to trigger only when a button is clicked, you will need to bind it to the click event of the button.

Very simple, isn’t it?

Do you know of any other ways to check for # (Hash) in URL using jQuery? Feel free to suggest by commenting below.

One Comment on jquery check for # (Hash) in URL

  1. 1

    Life saver, thanks a lot.
    Some things bugged out with a hash in url, but also bugged out if you set it to remove when there isn’t any.
    This a easy check!

Share your thoughts, comment below now!

*

*