Created
December 12, 2016 00:54
-
-
Save reimertz/6d50b0080d8b0e4098ec1a7849d67958 to your computer and use it in GitHub Desktop.
fetch content on hover, populate on click.
This file contains hidden or 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 linkContents = {}; | |
| document.addEventListener('mouseover', function(event) { | |
| var url = event.srcElement.href; | |
| if (event.srcElement.nodeName !== 'A') return | |
| else if (url.indexOf(location.href) === -1) return | |
| else if (linkContents[url]) return | |
| else fetchContent(url) | |
| }) | |
| document.addEventListener('click', function(event) { | |
| var url = event.srcElement.href; | |
| event.preventDefault(); | |
| event.stopPropagation(); | |
| if (event.srcElement.nodeName !== 'A') return | |
| else if (url.indexOf(location.href) >= 0) updateContent(url) | |
| }) | |
| function updateContent(url) { | |
| document.write(linkContents[url]) | |
| history.pushState({}, document.title, url.split('/').slice(-2)[0]); | |
| } | |
| function fetchContent(url) { | |
| var xhr = new XMLHttpRequest() | |
| linkContents[url] = true | |
| xhr.onreadystatechange = function() { | |
| if (xhr.readyState == XMLHttpRequest.DONE) { | |
| linkContents[url] = xhr.responseText | |
| } | |
| } | |
| xhr.open('GET', url, true); | |
| xhr.send(null); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment