Last active
January 13, 2020 11:43
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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