开发者

Grab immediate directory on URL

开发者 https://www.devze.com 2023-04-05 22:10 出处:网络
How can I use JavaScript to scrape the URL and then use the first directory o开发者_StackOverflow社区f the URL. So for example if I had a URL like domain.com/Organisations/Manage the bit I would want

How can I use JavaScript to scrape the URL and then use the first directory o开发者_StackOverflow社区f the URL. So for example if I had a URL like domain.com/Organisations/Manage the bit I would want to use is Organisations.

Note that this app can also run in development modes where it has prefixes on the url such as localhost:8888/AppName/Organisations/Manage

The reason for this is to apply the name to the body as an Id such as <body id="Organisations">

Any help would be much appreciated. Thanks


You'll need to slice up document.location.pathname. If "Organisations" always second from the last part of the path, then you could use this:

document.location.pathname.split("/").slice(-2, -1).toString();
//-> "Organisations"


This should do the trick as well:

document.location.origin + "/" + document.location.pathname.split("/")[1]
0

精彩评论

暂无评论...
验证码 换一张
取 消