I want to get the location in a URL (localhost: port/: location). For example, if the URL is localhost:8080/show/myplace/
I want to extract myplace
.
How to do this in javascript?
Asked
Active
Viewed 68 times
0
-
2`java` != `javascript`. From the `java` tag description: "*Java (not to be confused with JavaScript or JScript) [...]*" – Turing85 Jan 17 '18 at 10:24
-
2`window.location.pathname.split('/').slice(-1)[0]` – Set Kyar Wa Lar Jan 17 '18 at 10:27
3 Answers
0
Try below code
var location = window.location.pathname.split('/').slice(-1)[0];
This will return the part of URI you want.

jeet427
- 538
- 3
- 16
0
This will give you an array of each pathname in the url:
window.location.pathname.split('/')
https://jsfiddle.net/mhd28cre/ (check your console log when running the example)

Liam Kenneth
- 982
- 1
- 11
- 21