Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I have a URL : http://localhost:8080/school/teacher/reports/chapter-quiz/student
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
pop()
I'd say something like this?
var segments = location.pathname.split('/'); secondLastSegment = segments[segments.length - 2];
Add a comment
var pathArray = window.location.pathname.split( '/' );
var segment_1 = pathArray[1];
var segment_2 = pathArray[2];
var segment_3 = pathArray[3];
var segment_4 = pathArray[4];
console.log(segment_4) --> chapter-quiz
you can use this
var t = window.location.pathname.split("/") t.pop(); t.pop(); t.pop();
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
pop()twice?