Get protocol, domain, port, path, query & Hash using JS

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
5 min

banner

# Table of Contents

  1. Get protocol, domain, port, path, query & Hash using JS
  2. Using the URL interface to get the protocol, domain, port, path, query & Hash in JS
  3. Check if a URL is localhost in JavaScript

# Get protocol, domain, port, path, query & Hash using JS

You can use the window.location.protocol property to get the protocol from the current URL.

index.js
// ๐Ÿ‘‡๏ธ Note: protocol contains the colon const protocol = window.location.protocol; console.log(protocol); // ๐Ÿ‘‰๏ธ http:

getting the protocol of the page

The code for this article is available on GitHub

The location.protocol property returns a string that represents the protocol scheme of the URL, including the final colon :, e.g. http: or https:.

You can also access the protocol property on an <a> element to get the protocol of the link.

For example, if you have the following HTML.

index.html
<a id="my-anchor" href="https://bobbyhadz.com"> bobbyhadz.com </a>

You can access the protocol as follows.

index.js
const anchor = document.getElementById("my-anchor"); const result = anchor.protocol; // ๐Ÿ‘‰๏ธ 'https:'

If you need to get the port of the URL, use the window.location.port property.

index.js
console.log(window.location.port); // ๐Ÿ‘‰๏ธ 41867

getting the port of the url using javascript

The code for this article is available on GitHub

The window.location.port property returns a string that contains the port number of the URL.

If the URL doesn't contain an explicit port number, the property returns an empty string.

# Getting the host and the full URL of the page using JavaScript

If you need to get the host, use the window.location.host property.

index.js
// ๐Ÿ‘‡๏ธ Note: protocol contains the colon const protocol = window.location.protocol; console.log(protocol); // http: const host = window.location.host; console.log(host); // localhost:41867 const fullURL = window.location.protocol + '//' + window.location.host; console.log(fullURL); // http://localhost:41867

getting the protocol host and full url of the page

The code for this article is available on GitHub

The location.host property returns a string that contains the hostname, and then if the port of the URL is nonempty, a colon : and the port of the URL.

You can also get the full URL of the page by accessing window.location.href.

index.js
// ๐Ÿ‘‡๏ธ http://localhost:41867/ console.log(window.location.href);

get full url of page with window location href

The window.location.href property returns a string containing the whole URL.

There is also a window.location.origin property that achieves a similar goal.

index.js
// ๐Ÿ‘‡๏ธ http://localhost:41867 console.log(window.location.origin);

get origin from url using javascript

The window.location.origin property returns a string that contains the origin of the URL.

This consists of:

  • the scheme (http or https), followed by ://, followed by the domain, followed by :, followed by the port.

# Getting the query parameter string of the page using JavaScript

You can use the window.location.search property if you need to get the query parameter string of the page.

index.js
console.log(window.location.search); // ๐Ÿ‘‰๏ธ ?page=10&limit=5

get query parameter string of page using javascript

The code for this article is available on GitHub

The window.location.search property returns the query string.

The query string starts with a question mark ? and is followed by key=value pairs that are separated by ampersand & symbols.

# Getting the hash of the URL using JavaScript

If you need to get the hash # of the URL, use the window.location.hash property.

index.js
console.log(window.location.hash); // ๐Ÿ‘‰๏ธ #my-hash

get hash of url using javascript

The window.location.hash property returns a string containing a hash symbol # followed by the fragment identifier of the URL.

The fragment identifier is an ID on the page that the URL is trying to target.

# Getting the Path of the URL using JavaScript

If you need to get the path of the URL, use the window.location.pathname property.

index.js
// ๐Ÿ‘‡๏ธ '/blog/javascript-get-protocol-domain-port-path-query-hash' console.log(window.location.pathname);

get path of url using javascript

The code for this article is available on GitHub

The property returns a string that contains the path of the URL for the location.

If there is no path, the pathname property returns an empty string "" or a forward slash / depending on the browser's implementation.

Otherwise, a forward slash / followed by the path of the URL is returned.

Note that the query string or the hash is not included in the path.

# Using the URL interface to get the protocol, domain, port, path, query & Hash in JS

You can also use the URL interface to get the URL components.

The URL() constructor takes a URL as a parameter and returns a URL object that represents the given URL.

index.js
const fullURL = window.location.href; console.log(fullURL); // ๐Ÿ‘‰๏ธ http://localhost:41867/ const url = new URL(fullURL); const protocol = url.protocol; console.log(protocol); // ๐Ÿ‘‰๏ธ http: const port = url.port; console.log(port); // ๐Ÿ‘‰๏ธ 41867 const origin = url.origin; console.log(origin); // ๐Ÿ‘‰๏ธ http://localhost:41867
The code for this article is available on GitHub

Notice that we passed the current URL to the new URL() constructor via window.location.href.

You can also pass a different URL to the constructor.

index.js
const fullURL = 'https://bobbyhadz.com'; const url = new URL(fullURL); const protocol = url.protocol; console.log(protocol); // ๐Ÿ‘‰๏ธ https: const port = url.port; console.log(port); // ๐Ÿ‘‰๏ธ "" const origin = url.origin; console.log(origin); // ๐Ÿ‘‰๏ธ https://bobbyhadz.com

The available properties on the URL object are:

  • the hash # - a string containing a hash symbol # followed by the fragment identifier of the URL.
index.js
const fullURL = window.location.href; console.log(fullURL); // ๐Ÿ‘‰๏ธ http://localhost:41867#my-hash const url = new URL(fullURL); const hash = url.hash; console.log(hash); // ๐Ÿ‘‰๏ธ #my-hash
  • the host - a string containing the domain followed by a : and the port of the URL (if one exists).
index.js
const fullURL = window.location.href; console.log(fullURL); // ๐Ÿ‘‰๏ธ http://localhost:41867 const url = new URL(fullURL); const host = url.host; console.log(host); // ๐Ÿ‘‰๏ธ localhost:41867
  • the hostname - a string containing the domain of the URL.
index.js
const fullURL = window.location.href; console.log(fullURL); // ๐Ÿ‘‰๏ธ http://localhost:41867 const url = new URL(fullURL); const hostname = url.hostname; console.log(hostname); // ๐Ÿ‘‰๏ธ localhost
The code for this article is available on GitHub
  • href - a stringifier that returns a string containing the whole URL.
index.js
const fullURL = window.location.href; console.log(fullURL); // ๐Ÿ‘‰๏ธ http://localhost:41867 const url = new URL(fullURL); const href = url.href; console.log(href); // ๐Ÿ‘‰๏ธ http://localhost:41867/
  • origin - a string containing the origin of the URL (the scheme, domain and port).
index.js
const fullURL = window.location.href; console.log(fullURL); // ๐Ÿ‘‰๏ธ http://localhost:41867 const url = new URL(fullURL); const origin = url.href; console.log(origin); // ๐Ÿ‘‰๏ธ http://localhost:41867/
  • pathname - a string containing a leading forward slash / and then the path of the URL (excluding the query string or the hash).
index.js
const fullURL = window.location.href; console.log(fullURL); // ๐Ÿ‘‰๏ธ http://localhost:41867/blog/reverse-a-string const url = new URL(fullURL); const path = url.pathname; console.log(path); // ๐Ÿ‘‰๏ธ /blog/reverse-a-string
  • port - a string containing the port number of the URL.
index.js
const fullURL = window.location.href; console.log(fullURL); // ๐Ÿ‘‰๏ธ http://localhost:41867 const url = new URL(fullURL); const port = url.port; console.log(port); // ๐Ÿ‘‰๏ธ 41867
  • protocol - a string containing the protocol scheme of the URL (http or https) followed by a colon :.
index.js
const fullURL = window.location.href; console.log(fullURL); // ๐Ÿ‘‰๏ธ http://localhost:41867 const url = new URL(fullURL); const protocol = url.protocol; console.log(protocol); // ๐Ÿ‘‰๏ธ http:
  • search - the URL's query string beginning with a leading question mark ? character.
index.js
const fullURL = window.location.href; console.log(fullURL); // ๐Ÿ‘‰๏ธ http://localhost:41867?page=10&limit=5 const url = new URL(fullURL); const queryString = url.search; console.log(queryString); // ๐Ÿ‘‰๏ธ ?page=10&limit=5
The code for this article is available on GitHub
  • searchParams - a URLSearchParams object that can be used to access individual query parameters found in the search property.
index.js
const fullURL = window.location.href; console.log(fullURL); // ๐Ÿ‘‰๏ธ http://localhost:41867?page=10&limit=5 const url = new URL(fullURL); console.log(url.searchParams.get('page')); // 10 console.log(url.searchParams.get('limit')); // 5

I've also written an article on how to remove http or https from a URL using JavaScript.

# Check if a URL is localhost in JavaScript

Use the includes() method to check if a URL is localhost.

The includes method will return true if the URL is localhost, otherwise false is returned.

index.js
function isLocalhost(url) { return url.includes('localhost') || url.includes('127.0.0.1'); } // ๐Ÿ‘‡๏ธ true console.log(isLocalhost('http://localhost:3000/')); // ๐Ÿ‘‡๏ธ false console.log(isLocalhost('https://bobbyhadz.com'));

check if url is localhost

The code for this article is available on GitHub

You can use window.location.href instead of the hardcoded URL to get the current URL.

We created a reusable function that checks if a URL is localhost or not.

The String.includes() method returns true if the supplied substring is contained in the string and false otherwise.

We used the logical OR (||) operator, so for the function to return true, either condition has to be met.

  1. The URL has to contain the string localhost
  2. Or it has to contain the string 127.0.0.1

# Check if a URL is localhost using indexOf()

Alternatively, you can use the String.indexOf() method.

If the indexOf() method returns -1 for the strings localhost and 127.0.0.1, the URL is not localhost.

index.js
function isLocalHost(url) { return ( url.indexOf('localhost') !== -1 || url.indexOf('127.0.0.1') !== -1 ); } // ๐Ÿ‘‡๏ธ true console.log(isLocalHost('http://localhost:3000/')); // ๐Ÿ‘‡๏ธ false console.log(isLocalHost('http://example.com'));tp://example.com'));

check if url is localhost using indexof

The code for this article is available on GitHub

The String.indexOf() method returns the index of the first occurrence of a substring in a string.

If the substring is not contained in the string, the method returns -1.

If either of the calls to the indexOf() method returns an index that is not equal to -1, then the URL is localhost.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev