Skip to content

Instantly share code, notes, and snippets.

@dwsmart
Last active January 13, 2020 11:43
Show Gist options
  • Save dwsmart/bd6837f76c70bb499a2d81d50daa8960 to your computer and use it in GitHub Desktop.
Save dwsmart/bd6837f76c70bb499a2d81d50daa8960 to your computer and use it in GitHub Desktop.
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