I'm looking for a good way to easily count URL segments using JavaScript/jQuery?
For example:
www.myurl/segment1/segment2/segment3
should return 3
I'm looking for a good way to easily count URL segments using JavaScript/jQuery?
For example:
www.myurl./segment1/segment2/segment3
should return 3
- stackoverflow./questions/5321468/… – racecarjonathan Commented Feb 12, 2015 at 19:02
- 1 @racecarjonathan the answer you posted isn't related. I'm trying to count the url segments not get the last segment. I googled and didn't find a straightforward solution. – CyberJunkie Commented Feb 12, 2015 at 19:15
- 1 I see. My apologies. <3 lol – racecarjonathan Commented Feb 12, 2015 at 19:20
2 Answers
Reset to default 6var count = (location.pathname.split('/').length - 1) - (location.pathname[location.pathname.length - 1] == '/' ? 1 : 0);
var url = "www.myurl./segment1/segment2/segment3";
var segment = url.split("/").length - 1 - (url.indexOf("http://")==-1?0:2);
console.log(segment);
If number of "/" are segments for you