I need to run a JavaScript function when someone goes to https://website.com/custom/U1BJR09U. The U1BJR09U could be any string, but it needs to be passed on to the function. How should I go about implementing this?
Assuming that all traffic is redirected to a single point that serves a main file handling the URL changes, this should do it:
const hasCustom = window.location.pathname.match(/^\/custom\/([0-9A-Z]+)$/);
if (hasCustom && hasCustom[1]) {
const myString = hasCustom[1];
/* Do something with myString here */
}
Explanation of the regexp:
^ starts with\/ (escaped slash)custom\/ (another slash)([0-9A-Z]+) numbers and uppercase letters