Get Hash Value From Current Page URL In jQuery

Category > JQUERY || Published on : Friday, June 26, 2015 || Views: 6311 || Get Hash Value From Current Page URL In jQuery Get Hash Value Get Hash Value From Current Page URL


Introuction:-

In this tutorial we will learn How Get Hash Value From Current Page URL In jQuery using asp.net/c# code.

Description:-

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.

so lets starts the coding.

Get Hash Value From Current Page URL In JavaScript

In javascript we use the following codes to get the current page URL

var currentURL = window.location; 

In javascript we use the following codes to get the current page URL hash value from current page URL or Querystring

var currentHashURL = window.location.hash; 

Below is the complete example source codes that will demo you to get hash values of current page URL

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
      <title>How to 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>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr>
            <td>
                <strong>Click here to SET hash value to current URL: </strong>
                <a id="btnSetHashValue" href="#8743b52063cd84097a65d1633f5c74f5">Set Hash Value</a>
            </td>
        </tr>
        <tr>
            <td>
                <strong>Click here to GET hash value to current URL: </strong>
                <a id="btnGetHashValue" href="javascript:">Get Hash Value</a>
            </td>
        </tr>
    </table>
    </div>
    </form>
</body>
</html>

So In the javascript function we have used Substr function to extract values from the Address location and remove the first "#" character from the hash value.
Remember : To run above example, first you need to click on Set Hash Value link button to SET the hash value in current page url, then click on Get Hash Value link button to GET the hash value.