最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScriptjQuery count URL segments - Stack Overflow

programmeradmin5浏览0评论

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

Share Improve this question edited Feb 12, 2015 at 19:36 racecarjonathan 1,2321 gold badge11 silver badges22 bronze badges asked Feb 12, 2015 at 18:55 CyberJunkieCyberJunkie 22.7k61 gold badges154 silver badges219 bronze badges 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
Add a ment  | 

2 Answers 2

Reset to default 6
var 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

发布评论

评论列表(0)

  1. 暂无评论