Created
March 25, 2015 13:06
-
-
Save franzenzenhofer/f7f99fc784004c8d9e79 to your computer and use it in GitHub Desktop.
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 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