How to Get The Current URL in JQuery – Definitive guide

URL is the uniform resource locater and the current URL shows the address of the website.

You can get the current URL in JQuery using $(location).attr('href') statement.

Basic Example


When you check in the console, you’ll see the current URL of the page printed as shown below.


If You Want to Understand Details, Read on…

In this tutorial, you’ll learn the different methods available to get URLs in JQuery.

A URL contains different parts in it. Such as domain, port, path, and parameters. Let’s see the details of the URL structure with a sample URL.

When using JQuery, you need to use $() or JQuery() to denote its a jquery object and prefix # in the element id to access the element using its ID.

To use vanilla javascript to get the current URL, read How to Get the Current URL in Javascript?

Adding JQuery Reference

Add the below reference to the Jquery script.

Otherwise, you’ll get Uncaught ReferenceError: $ is not defined error when trying to access the elements using $.

<script src=""></script>

URl Strucuture and Sample URL

The following syntax shows the structure of the URL.


  • protocolhttp or https – Denotes the network protocol
  • domain – Denotes the name of your website. Here, it is
  • port – Port number on which the communication happens with the webserver. Most common value is 8080 for http connection and 443 for https connection. 443 is the schemas default port.
  • path – Denotes the path of the current page in the website. path is how-to-get-current-url-in-jquery/ for this page.
  • query – Additional parameters to pass values while communicating to the website. For example, you can use this to pass the username if you want to server different content for each user
  • hash – Denotes the anchor of the page. Normally, this contains any subheading of the page. When you use this, the page will be scrolled automatically to this section. Current URL

Using window.location.href

The href property of the location interface stringifies the whole URL of the current page and returns it to the caller.


Create a javascript method to return the current URL and reuse this method whenever you want to know the current URL.

Creating JS method to return a URL

function getURL() {
  return $(location).attr('href');

This method returns the current URL to the caller.

Get Domain From URL

When working with different domains, you may need to know the domain of the current URL.

  • The hostname property of the location interface returns the domain name of the current URL.


Use the following statement to log the domain name in the console.



Get Current URL Path

When working with web applications with different pages, you need to get the current URL path to customize some options based on the page.

  • The pathname property of the location interface returns the pathname of the current webpage.


Use the following statement to log the current webpage path in the console.




This is how you can get the path of the current URL in javascript.

Get URL Parameter

When working with applications that support customizations or caching, then you need to pass the options using the parameters to customize it.

For example, this website uses wp-rocket plugin for caching. When you want to see any uncached version of the page in this website, you need to pass the parameter npwprocket with value 1 as shown below.

URL With Parameter

To get the URL parameters in JQuery,

  • Use the search property in the location interface.
  • It will list all the parameters passed in the URL including the ? at the beginning.





Get URL without Parameters

If you want to get the full URL(Including domain and path) without parameters, there is no explicit property available.

  • Get the hostname and the pathname
  • Concatenate them together

For example, the following URL includes domain name, path, and a parameter.

Sample URL

To get only the URL with path, use the following code.


console.log($(location).attr('host') + $(location).attr('pathname'));

You’ll see the below output.


This is how you can get the URL excluding the parameters in JQuery.

Get URL Port

You need to get the port details from a URL for any developmental or configuration purposes.

  • The port property of the location interface returns the Port of the URL.


  • The default port of the https URL is 443. Hence, you’ll see the output as 443.
  • If you are using any other applications hosted in different server configurations, you’ll see the appropriate port.



This is how you can get the port number of the current URL in JQuery.

Get Last Part of URL

If you want to get the last part of the URL for any programmatic purposes, you can get it using the substring() method.

  • Find the last index of the /
  • Get the right side part of the split substring to get the last part of the URL


var url = $(location).attr('href');

console.log(url.substring(url.lastIndexOf('/') + 1));




This tutorial is available in this JSFiddle.

Additional Resources

Leave a Comment