Skip to content Skip to sidebar Skip to footer

How To Get The Last Part Of Url Angularjs

How to build the function in AngularJS to chceck the last part of the url? My examples: #/menu1/123/edit #/menu2/444/create/new #/menu3/sumbenu1/333/create/new #/menu4/submenu2/s

Solution 1:

This should give you the desired result:

window.alert(window.location.href.substr(window.location.href.lastIndexOf('/') + 1));

You could get the current URL by using: window.location.href Then you only need to get the part after the last '/'

Solution 2:

More efficient way:

location.pathname.split('/').slice(-1)[0]

In case of http://example.com/firstdir/seconddir/thirddir?queryparams#hash; it will give you thirddir

  • pathname will keep only the part of url after domain name and before query params
  • split will explode the url into array separated by /
  • slice will give you array containing only the last item
  • [0] will you last item as a string (in contrast to item of an array)

Post a Comment for "How To Get The Last Part Of Url Angularjs"