HomeLinuxJavaScript – Get Portion of URL Path

JavaScript – Get Portion of URL Path


Whereas testing an online web page or the location, there is usually a requirement to extract the URL of various net pages on the developer’s finish. As an illustration, storing the URL corresponding to every net web page within the related code to entry them or make the most of the added functionalities sooner or later. In such case eventualities, getting the portion of the URL path is handy for managing the assets successfully.

This text will talk about the approaches to get the portion of the URL path in JavaScript.

Tips on how to Get a Portion of URL Path Utilizing JavaScript?

To get the portion of URL path utilizing JavaScript, contemplate the below-given approaches:

Technique 1: Get Portion of URL Path Utilizing “location.host” and “pathname” Properties in JavaScript

The “location.host” property returns the IP handle and port of a URL. The “pathname” property provides the pathname of a URL. These properties could be utilized to separate the IP handle and path identify within the URL and fetch them individually.

Instance

Overview the below-given demonstration:

<script>
let a = window.location.host
let b = window.location.pathname
console.log(‘The primary portion of the URL is : ‘, a);
console.log(‘The second portion of the URL is : ‘, b);
</script>

Within the above demonstration:

  • Firstly, apply the “host” property to fetch the IP handle.
  • Likewise, get the trail identify contained within the URL by way of the “pathname” property
  • Lastly, show the fetched parts from the URL on the console.

Output

Within the above output, it may be noticed that the IP handle and the trail have been fetched individually from the redirected URL.

Technique 2: Get the Portion of URL Path Utilizing the Mixture of the “break up()” and “slice()” Strategies in JavaScript

The “break up()” technique splits a string right into a substring array primarily based on the parameters and the “slice()” technique extracts part of the string. These strategies could be utilized together with the “pathname” property to fetch the trail identify from the URL primarily based on the parameter of the utilized strategies.

Syntax

string.break up(separator, restrict)

Within the given syntax:

  • separator” factors to the string that must be used for splitting.
  • restrict” refers back to the integer that limits the variety of splits.

Within the above syntax:

  • begin” and “finish” point out the beginning and finish positions, respectively.

Instance

Let’s undergo the next strains of code to know the idea clearly:

<script>
let myLink = new URL(“http://www.google.com/residence/section1”);
let myPortion = myLink.pathname.break up(‘/’).slice(1);
console.log(‘The parts within the URL are : ‘, myPortion);
</script>

Within the above code snippet:

  • Firstly, create the brand new URL object utilizing the “new” key phrase and the “URL()” constructor, respectively to characterize the desired URL.
  • Within the subsequent step, affiliate the “break up()” and “slice()” strategies with the “pathname” property such that the trail identify is extracted from the desired URL primarily based on the given separator and restrict, respectively.
  • Lastly, show the parts of the trail identify within the URL as an array on the console.

Output

Within the output, it may be observed that the parts of the trail from the URL are returned as an array.

Conclusion

To get the portion of the URL path in JavaScript, apply the “location.host” and “pathname” properties or the “slice()” and “break up()” strategies. The previous approaches fetch the IP handle and path identify individually from the URL. The latter strategies could be utilized to get the parts of the trail identify within the URL. This write-up mentioned the approaches to get the portion of the URL path.

In regards to the creator

Umar Hassan

I’m a Entrance-Finish Net Developer. Being a technical creator, I attempt to be taught new issues and adapt with them on daily basis. I’m passionate to put in writing about evolving software program instruments and applied sciences and make it comprehensible for the end-user.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments