Web Design Blog

This is where we store some of our Web Development thoughts, tips and tricks, just because we like to share.

Javascript – Get The Hash Tag Value From A URL

So, you have the following URL:

http://www.example.com/page/#comment-12

You want to get the value after the hash tag (#), which is comment-12

The following Javascript code will help you achieve what you’re after:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
<script type="text/javascript">
 
//check if hash tag exists in the URL
if(window.location.hash) {
 
 //set the value as a variable, and remove the #
 var hash_value = window.location.hash.replace('#', '');
 
 //show the value with an alert pop-up
 alert(hash_value);
 
}
</script>

That’s it. It’s as easy that.

Please let me know if it did or didn’t work for you. Alternatively, if you know of a better way to achieve the same output. Thanks in advance!

02 Sep 2011 / 1 Comment / Javascript / by Maruf

1 Comment

  1. Roy
    11/11/2011
    1

    Thanks for this script,

    Works like a charm!

Leave a Reply

© 2012 BrightCherry :)