Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Add a string to the start of all internal links on a page.
//get base url
const getUrl = window.location;
const baseUrl = getUrl .protocol + "//" + + "/";
// 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
// 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