This post will discuss how to get the current URL of the page in JavaScript and jQuery.

1. Using JavaScript

The Location object in JavaScript contains information about the current URL of the document. It can be accessed using the Window interface. So, you can use the Window.location to get the Location object for the current document. Then href property location.href can be used to get a string containing the full URL.

The Location object offers several other properties to get protocol, host, domain, port number, pathname, etc., of the URL. For example,

 
Alternatively, you can access the Location object with the Document.location property of the Document interface. You can also use the document.URL or document.documentURI property to get the URL of the current document.

2. Using jQuery

With jQuery, you can use the .prop() or .attr() method to get properties of the location object, as shown below:

That’s all about getting the current URL in JavaScript and jQuery.