How to Get the Current URL in Javascript – Definitive guide

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

You can get the current URL in javascript using window.location.href statement.

Basic Example

console.log(window.location.href);

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

Output

https://www.jsowl.com/how-to-get-current-url-in-javascript

In this tutorial, you’ll learn the different methods available to get the current URL in Javascript.

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.

To use JQuery to get the current URL, read How to Get Current URL in JQuery?

URl Strucuture and Sample URL

The following syntax shows the structure of the URL.

Syntax

<protocol>//<domain>:<port>/<path>/<query><hash>
  • protocolhttp or https – Denotes the network protocol
  • domain – Denotes the name of your website. Here, it is jsowl.com
  • 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-javascript/ 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
https://www.jsowl.com/how-to-get-current-url-in-javascript/

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.

window.location.href

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 window.location.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.

Code

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

console.log(window.location.hostname)

Output

jsowl.com

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.

Code

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

console.log(window.location.pathname)

Output

how-to-get-current-url-in-javascript/

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, you need to pass the options using the parameters to customize it.

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

URL With Parameter

https://www.jsowl.com/how-to-get-current-url-in-javascript/?nowprocket=1

To get the URL parameters in Javascript,

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

Code

console.log(window.location.search)

Output

?nowprocket=1

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 both values.

Sample URL

The following URL includes domain name, path, and a parameter.

https://www.jsowl.com/how-to-get-current-url-in-javascript/?nowprocket=1

Code

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

console.log(location.host + location.pathname)

Output

https://www.jsowl.com/how-to-get-current-url-in-javascript/

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

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.

Code

console.log(window.location.port)
  • 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.

Output

443

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

Get Last Part of URL

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 /
  • Get the right side part of the split substrings to get the last part of the URL.

Code

var url = window.location.href;

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

Output

how-to-get-current-url-in-javascript/

JSfiddle

This tutorial is available in this JSFiddle.

Additional Resources

Leave a Comment