0

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?

vindev
  • 2,240
  • 2
  • 13
  • 20
noName94
  • 3,783
  • 1
  • 16
  • 32

3 Answers3

0

I think that :

window.location.pathname

should work

Aznhar
  • 610
  • 1
  • 10
  • 30
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