tags:

views:

129

answers:

1

Here is my url:

https://sub.domain.com/core/subsite/piano/page/...

I need to take the string 'piano', and then apply it to elements on the page. The string will not always be 'piano', i need to take the 3rd section of the URL.

(Below is what I currently have which uses the breadcrumb of the page to identify what page the users is currently viewing) however I'd like to take 'piano' as the class to apply:

$(function() {
    $('span .breadcrumb').each(function(){
        $('#Nav').addClass($(this).text());
        $('#Content').addClass($(this).text());
        $('.xpanding_footer').addClass($(this).text())
        $('#footer').addClass($(this).text());
    });
});
+1  A: 

I can only guess that you are asking about third parameter in pathname when you are splitting using backslash.

So, you can get the value using:

window.location.pathname.split('/')[3]
bluszcz
Could you elaborate on how I might go about using this, some kind of example would be appreciated.
danit
`window.location.pathname` is your url, excluding host name: `/core/subsite/piano/page/`. `split('/')` divides a string into an array, where the `/` is the delimiter for each new array position; thus: `["","core","subsite","piano","page",""]`. The `[3]` is accessing the fourth position (first pos being `[0]`) within that array, i.e. `piano`, and from there, you can store that as a variable, and do whatever you want with it.
David Hedlund