Skip to content

Instantly share code, notes, and snippets.

@ryantownsend
Last active March 14, 2018 14:37
Show Gist options
  • Select an option

  • Save ryantownsend/a7039e4a0cbc5fc00ef0deef0d7dca93 to your computer and use it in GitHub Desktop.

Select an option

Save ryantownsend/a7039e4a0cbc5fc00ef0deef0d7dca93 to your computer and use it in GitHub Desktop.
JavaScript to prefetch marked links after page load
<script>
function(w, d) {
var prefetchLinks = function() {
d.querySelectorAll("a[href][rel='prefetch']").forEach(function() {
var link = d.createElement('link');
link.rel = 'prefetch';
link.href = this.getAttribute('href');
d.head.appendChild(link);
});
}
w.addEventListener('load', prefetchLinks);
})(window, document);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment