How to get the current URL with JavaScript?

Query:

All I want is to get the website URL. Not the URL as taken from a link. On the page loading, I need to be able to grab the full, current URL of the website and set it as a variable to do with as I please.

How to get the current URL with JavaScript?

Use:

window.location.href

The line below works, but it is bugged for Firefox.

document.URL

URL Info Access

JavaScript provides you with many methods to retrieve and change the current URL, which is displayed in the browser’s address bar. All these methods use the Location object, which is a property of the Window object. You can create a new Location object that has the current URL as follows:

var currentLocation = window.location;

Basic URL Structure

<protocol>//<hostname>:<port>/<pathname><search><hash>
  • protocol: Specifies the protocol name be used to access the resource on the Internet. (HTTP (without SSL) or HTTPS (with SSL))
  • hostname: Host name specifies the host that owns the resource. For example, www.stackoverflow.com. A server provides services using the name of the host.
  • port: A port number used to recognize a specific process to which an Internet or other network message is to be forwarded when it arrives at a server.
  • pathname: The path gives info about the specific resource within the host that the Web client wants to access. For example, /index.html.
  • search: A query string follows the path component, and provides a string of information that the resource can utilize for some purpose (for example, as parameters for a search or as data to be processed).
  • hash: The anchor portion of a URL, includes the hash sign (#).

With these Location object properties you can access all of these URL components and what they can set or return:

  • href – the entire URL
  • protocol – the protocol of the URL
  • host – the hostname and port of the URL
  • hostname – the hostname of the URL
  • port – the port number the server uses for the URL
  • pathname – the path name of the URL
  • search – the query portion of the URL
  • hash – the anchor portion of the URL

Get URL of a web page in JavaScript:

Getting the full URL of the current page is easy using pure JavaScript. For example, try this code on this page:

window.location.href;
// use it in the console of this page will return
// 'https://programming-articles.com/how-to-get-the-current-url-with-javascript/'

The window.location.href property returns the URL of the current page.

document.getElementById("root").innerHTML = "The full URL of this page is:<br>" + window.location.href;
<!DOCTYPE html>
<html>

<body>
  <h2>JavaScript</h2>
  <h3>The window.location.href</h3>
  <p id="root"></p>
</body>

</html>

Just not bad to mention these as well:

  • if you need a relative path, simply use window.location.pathname;
  • if you’d like to get the host name, you can use window.location.hostname;
  • and if you need to get the protocol separately, use window.location.protocol
    • also, if your page has hash tag, you can get it like: window.location.hash.

So window.location.href handles all in once… basically:

window.location.protocol + '//' + window.location.hostname + window.location.pathname + window.location.hash === window.location.href;
    //true

Also using window is not needed if already in window scope.

So, in that case, you can use:

location.protocol

location.hostname

location.pathname

location.hash

location.href

Adding screenshot and results for quick reference:

Results:

window.location;

Location {ancestorOrigins: DOMStringList, href: 'https://programming-articles.com/how-to-get-the-current-url-with-javascript/', origin: 'https://programming-articles.com', protocol: 'https:', host: 'programming-articles.com', …}

document.location


Location {ancestorOrigins: DOMStringList, href: 'https://programming-articles.com/how-to-get-the-current-url-with-javascript/', origin: 'https://programming-articles.com', protocol: 'https:', host: 'programming-articles.com', …}

window.location.pathname

'/how-to-get-the-current-url-with-javascript/'

window.location.href

'https://programming-articles.com/how-to-get-the-current-url-with-javascript/'

location.hostname

'programming-articles.com'

Hope you learned something from this post.

Follow Programming Articles for more!

About ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ

Linux and Python enthusiast, in love with open source since 2014, Writer at programming-articles.com, India.

View all posts by ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ →