Get parts of URL after domain name //... by splitting URL into array

You could use array length if you don't have a fixed size or number of elements.

var path = array[array.length-2]+'/'+array[array.length-1];

If you just want the path use plain JS or jQuery as they suggest you in comments.

//Plain JS
var path = window.location.pathname;
//jQuery
$(location).attr('pathname'); 

You don't need any of this, you just want window.location.pathname:

> window.location.pathname
"/questions/11898626/get-items-of-the-array/11898963"

This will let you in the future have directories like "portfolio/2012/ruimzicht.html", and change domains to say "www.mikevierwind.???" without changing your code.


If you are not currently on the domain (and can't do the above), you can do it your way with a one-liner:

> pathArray.slice(-2).join('/')
"portfolio/ruimzicht.html"

But this is not future-proof like the above. To make it future-proof, you can do:

> url.split(document.domain)[1].slice(1)
"portfolio/2012/ruimzicht.html"

One would do this generally on foreign URLs when you are not currently on the domain and thus can't do window.location.pathname.