Is there URL class in JavaScript?

Yes! There is now a standard, broadly compatible, URL class for Javascript.

The constructor takes a url parameter, and an optional base parameter to use as a base if the url parameter is a relative URL

const url = new URL(window.location.href);
console.log(url.hostname); // "www.example.com"
console.log(url.pathname); // "/cats"

James Padolsey had tackled this problem.

In a nutshell, you can create an anchor element using document.createElement(), and several of its native properties are then easily accessible, such as protocol, port, and hostname.

For more information: http://james.padolsey.com/javascript/parsing-urls-with-the-dom/

Tags:

Javascript

Url