Search code examples
javascriptdirectoryparenthtml

How to print a certain level directory of webpage using javascript?


Say I have a website:

website.com/categories/stuff/morestuff

How would i print 'stuff' if I am in 'morestuff' currently. Meaning print the second directory or third and so on. Thanks.


Solution

  • get the string document.location.href, split it at / characters, and then use the second to last string in the array

    var a = document.location.href.split('/');
    alert (a[a.length-2]);