0

I'm creating a website that needs to get the folder name and output it to a var or str.

Example URL: https://myurl.com/my-folder

Output: my-folder

My Code:

<script>
 var wname = how I get the folders name;
 document.getElementById('workspacename').innerHTML=wname;
</script>
<span class/id="workspacename"></span>
Nathanna
  • 53
  • 10
  • 1
    Possible duplicate of [How can i get the name of an html page in Javascript?](https://stackoverflow.com/questions/16611497/how-can-i-get-the-name-of-an-html-page-in-javascript) – aaplmath Aug 26 '18 at 22:22

1 Answers1

1

In javascript you can use the location global. The location global contains quite a bit of information, if you'd like to see whats available you can read the MSDN or open your browsers console and type "location." to see suggestions and console.log them.

var path = location.pathname;

You can pair this with substring if you have trailing names in your path.

For example if you wanted 'workspace' from /workspace/some-random-string

You could do

let str = location.pathname.substring(1, location.pathname.length); //removes first /
str = str.substring(0, str.indexOf('/'));//substring from 0 to first /