I have a URL : http://localhost:8080/school/teacher/reports/chapter-quiz/student
Getting the last segment, I just need to do this
var lastSegment = location.pathname.split('/').pop();
But how do I grab the one next to the last one ? chapter-quiz
I have a URL : http://localhost:8080/school/teacher/reports/chapter-quiz/student
Getting the last segment, I just need to do this
var lastSegment = location.pathname.split('/').pop();
But how do I grab the one next to the last one ? chapter-quiz
Share Improve this question edited Jul 18, 2015 at 23:02 Patel 1,4781 gold badge13 silver badges24 bronze badges asked Jul 18, 2015 at 22:59 code-8code-8 58.9k120 gold badges391 silver badges670 bronze badges 2-
Can't you just
pop()
twice? – Frank Bryce Commented Jul 18, 2015 at 23:54 - Maybe this old fiddle of mine can help you – Tim Vermaelen Commented Jul 19, 2015 at 4:25
3 Answers
Reset to default 4I'd say something like this?
var segments = location.pathname.split('/');
secondLastSegment = segments[segments.length - 2];
Split the segments
var pathArray = window.location.pathname.split( '/' );
Create Variables
var segment_1 = pathArray[1];
var segment_2 = pathArray[2];
var segment_3 = pathArray[3];
var segment_4 = pathArray[4];
Use it
console.log(segment_4) --> chapter-quiz
you can use this
var t = window.location.pathname.split("/")
t.pop();
t.pop();
t.pop();
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745100369a4611235.html
评论列表(0条)