Skip to content

Instantly share code, notes, and snippets.

@franzenzenhofer
Created March 25, 2015 13:06
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 franzenzenhofer/f7f99fc784004c8d9e79 to your computer and use it in GitHub Desktop.
Save franzenzenhofer/f7f99fc784004c8d9e79 to your computer and use it in GitHub Desktop.
var allreadyPrefetched = {};
var preFetcher = function(elem) {
if(elem && elem.href && !allreadyPrefetched[elem.href])
{
var hint;
hint = document.createElement("link");
hint.rel = "prefetch";
hint.href = elem.href;
document.head.appendChild(hint);
allreadyPrefetched[elem.href]=true;
}
return elem;
};
var delay = function (elem, callback) {
var timeout = null;
elem.onmouseover = function() {
// Set timeout to be a timer which will invoke callback after 1s
//console.log('mouseover');
timeout = setTimeout(callback, 300);
};
elem.onmouseout = function() {
// Clear any timers set to timeout
//console.log('mouseout');
clearTimeout(timeout);
};
};
(function() {
$('a[href*="' + window.location.hostname + '"], a:not([href^="http"])').on('mousedown touchstart', preFetcher);
$('a[href*="' + window.location.hostname + '"], a:not([href^="http"])').each(
function(){
//console.log(this)
var that = this;
delay(that, function(){
//console.log('in pre prefetcher');
//console.log(that);
preFetcher(that);
});
});
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment