Home Asp.net Get Hash Value From Current Page URL In jQuery

Get Hash Value From Current Page URL In jQuery

62
0
Get Hash Value From Current Page URL In jQuery
Get Hash Value From Current Page URL In jQuery

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:

var currentURL = window.location;

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:

var currentHashURL = window.location.hash;

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:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Get Current Page URL Hash Value In jQuery</title>
    <script type="text/javascript" src="https://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.

Previous articleHandle Multiple Submit Buttons in Single MVC Form
Next articleGet Files List From Directory Recursively in C# Vb.net
Hi there, I am Mayank, the man behind Technical Mack. I started AspnetO with a motive to educate people on various programming languages ranging from beginners to expert level. Through this blog, I aim to provide more insightful content.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

three + 11 =