Add a string to the start of all internal links on a page.
//get base url | |
const getUrl = window.location; | |
const baseUrl = getUrl .protocol + "//" + getUrl.host + "/"; | |
// the string you want to append | |
const stringToAdd = "test/" | |
// get all a tags | |
let elements = document.getElementsByTagName('a'); | |
for (let node of elements) { | |
// is this internal to the site? If so update the link | |
if(node.href.startsWith(baseUrl)){ | |
// update the link with the stringToAdd | |
node.href = baseUrl + stringToAdd + node.href.replace(baseUrl,"") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment