我正在寻找一种使用JavaScript / jQuery轻松统计网址细分的好方法吗?
例如:
www.myurl.com/segment1/segment2/segment3
应该返回3
答案 0 :(得分:2)
var url = "www.myurl.com/segment1/segment2/segment3";
var segment = url.split("/").length - 1 - (url.indexOf("http://")==-1?0:2);
console.log(segment);
如果“/”的数量是您的分段
答案 1 :(得分:1)
var count = (location.pathname.split('/').length - 1) - (location.pathname[location.pathname.length - 1] == '/' ? 1 : 0);