Powered by Blogger.

Saturday, October 28, 2017

How to get current page URL using jQuery in asp.net?

Here we will discuss how we can get current page URL using jQuery in asp.net. Recently we got one requirement to retrieve the current page using jQuery in asp.net.

To get started with asp.net, let us create a web site first.

Also you can see my previous posts on: Difference between asp.net and asp.net core, also read Clear textboxes inside a form in C#.Net, Get all time zones in C#.Net and Convert ArrayList to String using C#.Net.

Open Visual Studio 2015 -> File -> New -> Web Site...
Then in the New Web Site dialog box, then choose ASP.NET Web Forms Site template
from Templates -> Visual C#.

jQuery get page URL in asp.net

Then in the Default.aspx page, write the below code. On the button click it will display the current page URL.

Below is the jQuery code.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#btnGetURL").on("click", function () {
                var pageURL = $(location).attr("href");
    <br />
    <div class="row">
        <div class="col-md-4">
            <input type="button" id="btnGetURL" value="Get Current Page URL" /><br />


Once you click on the button, you can see the current page URL like below:

Asp.Net get page URL using jQuery