Now here in this tutorial, I’ll explain how to read or get hash value from current page URL or QueryString in client-side using JavaScript or jQuery in Asp.net with example code.
In my previous tutorials, I’d explained how to get querystring value in jquery, passing ampersand in querystring, pass multiple parameters in querystring, get gridview selected row hiddenfield value using jquery here.
Get Hash Value From Current Page URL In JavaScript
To get the current page URL, we just need to write following single line of javascript statement like:
If you want to get the value from current page URL or QueryString, we just need to add .hash in the above javascript statement like:
Get Hash Value From Current Page URL Using JavaScript or jQuery
Following is the complete example code that I have used for the demonstration to get it from current page url:
<head id=”Head1″ runat=”server”>
<title>Get Current Page URL Hash Value In jQuery</title>
<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js”>
</script>
<script type=”text/javascript”>
$(document).ready(function () {
$(“a#btnGetHashValue”).click(function () {
alert(window.location.hash.substr(1));
});
});
</script>
</head>
<body>
<table>
<tr>
<td>
<strong>Click to SET hash value to current URL: </strong>
<a id=”btnSetHashValue” href=”#blahhhh”>Set Hash Value</a>
</td>
</tr>
<tr>
<td>
<strong>Click to GET hash value to current URL: </strong>
<a id=”btnGetHashValue” href=”javascript:”>Get Hash Value</a>
</td>
</tr>
</table>
</body>
</html>
As you can see from the above code, I’ve used substr(1) in window.location.hash.substr(1) under alert box which will remove the first “#”. If you want to get the value with “#”, just remove the substr(1) from window.location.hash.substr(1) in alert.
Note: To run above example, first we need to click on Set Hash Value link button to SET it in current page url, then click on Get Hash Value link button to GET it.