Last segment of URL with JavaScript

You can also use the lastIndexOf() function to locate the last occurrence of the / character in your URL, then the substring() function to return the substring starting from that location:

console.log(this.href.substring(this.href.lastIndexOf("https://stackoverflow.com/") + 1));

That way, you’ll avoid creating an array containing all your URL segments, as split() does.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)