Created
March 25, 2015 13:05
-
-
Save franzenzenhofer/154930a1f07830f9aeb7 to your computer and use it in GitHub Desktop.
injecting internal prefetch hints
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