Skip to content

Instantly share code, notes, and snippets.

@ymschaap
Last active June 22, 2019 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ymschaap/412121f457432170e5b3af99b971b07a to your computer and use it in GitHub Desktop.
Save ymschaap/412121f457432170e5b3af99b971b07a to your computer and use it in GitHub Desktop.
Segment DOM link types
var nodes = document.getElementsByTagName("a");
var link = document.createElement("a");
var internal = 0;
var external = 0;
var hash = 0;
var navigateHash = 0;
var issue = 0;
if (nodes) {
for (var i = 0, len = nodes.length; i < len; i++) {
var node = nodes[i];
// our local parser trick
if (node.href) {
link.href = node.href;
if (document.location.hostname === link.hostname) {
internal++;
if (document.location.pathname === link.pathname && link.hash.length > 0) {
// check if hash matches an element in the DOM (scroll to)
var element = document.querySelector(link.hash);
if (element) {
hash++;
} else {
navigateHash++;
}
}
} else if (document.location.hostname !== link.hostname) {
external++;
} else {
issue++;
}
}
}
}
console.log({ internal, external, hash, navigateHash, issue });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment