Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
77
rated 0 times [  81] [ 4]  / answers: 1 / hits: 27858  / 12 Years ago, fri, august 10, 2012, 12:00:00

I have make this code:



var newURL = $(.list-portfolio a).attr(href),
pathArray = newURL.split( '/' ),
secondLevelLocation = pathArray[0];
console.log(pathArray);

var pathArray = pathArray[3, 4];


The pathArray value is [http:, , www.mikevierwind.nl, portfolio, ruimzicht.html]



How can i get the last 2 items of this array. I want that the results is portfolio/ruimzicht.html.


More From » jquery

 Answers
229

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.


[#83721] Wednesday, August 8, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dezmondhumbertob

Total Points: 79
Total Questions: 112
Total Answers: 107

Location: Morocco
Member since Fri, May 22, 2020
4 Years ago
;