Menu

How to get a segment from a URL using JS?

We can find a text or get a segment from a URL or web address using the JS split and slice function. Below is an example of a slice and split function in simple JavaScript code.

Lets assume we have a URL: https://rashidjorvee.blogspot.com/content/2015/javascript:getName_function/12032/live.html

When we run the split based on colon(:) it will return as below.

e.g. window.location.href.split(":").pop()

result ==> getName_function/12032/live.html

Now, from this if you want to read only few characters from any index, then do the slice on the URL.

e.g. window.location.href.split(":").pop().slice(0,7);

result ==> getName

No comments:

Post a Comment