Skip to content

Instantly share code, notes, and snippets.

@tranchausky
Forked from jserrao/url-segment.js
Created June 29, 2020 03:03
Show Gist options
  • Save tranchausky/e13051dff44bec317b8f8f0bddca5a72 to your computer and use it in GitHub Desktop.
Save tranchausky/e13051dff44bec317b8f8f0bddca5a72 to your computer and use it in GitHub Desktop.
Get Last Segment of a URL in Javascript
var pageURL = window.location.href;
var lastURLSegment = pageURL.substr(pageURL.lastIndexOf('/') + 1);
console.log(lastURLSegment);
@tranchausky
Copy link
Author

tranchausky commented Jun 29, 2020

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment