-
-
Save tranchausky/e13051dff44bec317b8f8f0bddca5a72 to your computer and use it in GitHub Desktop.
Get Last Segment of a URL in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pageURL = window.location.href; | |
var lastURLSegment = pageURL.substr(pageURL.lastIndexOf('/') + 1); | |
console.log(lastURLSegment); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://domain.com/collections/2000_mercedes-benz_c280_2-pin-rear-pads/?akasdf=1jsadjf
réult
collections/2000_mercedes-benz_c280_2-pin-rear-pads
// new function for collection - data - shopify
function getYMMOFromRequestUrl() {
var pathname = window.location.pathname;
pathname = pathname.trim()
pathname = pathname.toLowerCase();
//xoa ky tu / cuoi cung
var slash = pathname.substr(-1);
if (slash == '/') {
pathname = pathname.slice(0, -1)
}
var lastPathname = pathname.substring(pathname.lastIndexOf('/') + 1);
var listYMMO = lastPathname.split('_');
// console.log(listdata)
// console.log(listdata.length)
var YMMO = {};
YMMO.year = '';
YMMO.make = '';
YMMO.model = '';
YMMO.submodel = '';
if (listYMMO.length == 4) {
YMMO.year = upFistCase(listYMMO[0]);
YMMO.make = upFistCase(listYMMO[1]);
YMMO.model = upFistCase(listYMMO[2]);
YMMO.submodel = upFistCase(listYMMO[3]);
}
return YMMO;
}