d

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore.

15 St Margarets, NY 10033
(+381) 11 123 4567
ouroffice@aware.com

 

KMF

How To Get the Current URL With Javascript

In Javascript, it is sometimes necessary to get the URL of the current web page you are on. There are a few ways to do this.

How to Get the Current URL With Javascript

If we want to get the full URL of the current web page, we simply have to use window.location.href.

// Shows us the current URL
let currentUrl = window.location.href;

How to Get the Current Domain URL With Javascript

If you only want to get the current domain, excluding any pages, you can run the following in Javascript:

let currentDomain = window.location.origin;

For instance, if you were on https://google.com/page/search/go/, the above would simply return https://google.com.

How To Get the Path After the Domain With JavaScript

If you want to get only the path after the domain, then run the following:

let currentPath = window.location.pathname;

Although these are the most common uses for window.location, there are also a number of other useful properties, such as:

  • window.location.port – the current port
  • window.location.protocol – the current protocol, i.e. https:
  • window.location.hash – anything after a hash at the end of the URL
  • window.location.hostname – the domain without any ports or protocols

Credit: Source link

Previous Next
Close
Test Caption
Test Description goes like this