Display the current page's hash/anchor in the page's body with JavaScript
-
13-06-2021 - |
Question
Say a user visits the page http://somedomain.com/path/file.html#foo, I'd like to display, using JavaScript, somewhere on the page a text like:
→ foo
Solution
Use
window.location.hash
to grab the anchor's name.var anchorName = window.location.hash;
Create a text node to display what you want:
var path = document.createTextNode("→ " + anchorName);
Add it to the Dom:
document.getElementById(idOfContainerYouWantToEdit).appendChild(path);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow