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,164
  • 2
  • 12
  • 20
anonymous_siva
  • 3,349
  • 1
  • 15
  • 28

3 Answers3

0

I think that :

window.location.pathname

should work

Aznhar
  • 532
  • 1
  • 9
  • 28
0

Try below code

var location = window.location.pathname.split('/').slice(-1)[0];

This will return the part of URI you want.

jeet427
  • 510
  • 3
  • 11
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
  • 984
  • 1
  • 11
  • 21