Last Segment before the question mark ( ? ) in Javascript -
here url
http://localhost:8080/bim/teacher/reports/section-exercise/assignment?assessmentid=206a9246-ce83-412b-b8ad-6b3e28be44e3&classroomid=722bfadb-9774-4d59-9a47-89ac9a7a8f9a
i want grab last segment of url before ?
, ignore after ?
can please teach me how ?
i've tried
var href = location.href; var lastsegment = href.substr(href.lastindexof('/') + 1); console.log(lastsegment);
i got
assignment?assessmentid=206a9246-ce83-412b-b8ad-6b3e28be44e3&classroomid=722bfadb-9774-4d59-9a47-89ac9a7a8f9a
i want assignment
try this
var pathparts = location.pathname.split('/'), basename = pathparts[pathparts.length - 1];
or super handy one-liner (sometimes have remind myself arrays have methods)
var basename = location.pathname.split('/').pop();
Comments
Post a Comment