Last active
June 22, 2019 17:50
-
-
Save ymschaap/412121f457432170e5b3af99b971b07a to your computer and use it in GitHub Desktop.
Segment DOM link types
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
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