Comparing version 0.1.3 to 0.2.0
{ | ||
"name": "prefetch", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"description": "Customizable interaction-based link prefetching.", | ||
@@ -5,0 +5,0 @@ "main": "prefetch.js", |
@@ -21,2 +21,3 @@ /** | ||
self.$exclusions = config.exclusions || []; | ||
self.$callback = config.callback; | ||
config.containers = config.containers || []; | ||
@@ -35,7 +36,7 @@ self.addContainers(config.containers); | ||
for(var i = 0; i < a.length; ++i){ | ||
injectPrefetchLink(a[i]); | ||
execute(a[i]); | ||
} | ||
} | ||
else{ | ||
injectPrefetchLink(a); | ||
execute(a); | ||
} | ||
@@ -120,9 +121,16 @@ } | ||
function injectPrefetchLink(a){ | ||
if(!a) return; | ||
function injectPrefetchLink(url){ | ||
var link = createLinkTag(url); | ||
document.getElementsByTagName('head')[0].appendChild(link); | ||
} | ||
function execute(a){ | ||
if(!a || !a.href) return; | ||
var url = (typeof a === 'object') ? a.href : a; | ||
var link = (url) ? createLinkTag(url) : null; | ||
if(link){ | ||
document.getElementsByTagName('head')[0].appendChild(link); | ||
if(self.$callback){ | ||
self.$callback(url, a, injectPrefetchLink); | ||
} | ||
else{ | ||
injectPrefetchLink(url); | ||
} | ||
if(typeof a === 'object'){ | ||
@@ -136,3 +144,3 @@ a.setAttribute('data-no-prefetch', ''); | ||
var a = getLinkTarget(e.target); | ||
injectPrefetchLink(a); | ||
execute(a); | ||
} | ||
@@ -143,3 +151,3 @@ | ||
var a = getLinkTarget(e.target); | ||
injectPrefetchLink(a); | ||
execute(a); | ||
} | ||
@@ -153,3 +161,3 @@ | ||
if(!self.$delayBeforePrefetch){ | ||
injectPrefetchLink(a); | ||
execute(a); | ||
} | ||
@@ -156,0 +164,0 @@ else{ |
@@ -13,5 +13,8 @@ # prefetch | ||
* No external dependencies | ||
* Prefetch links based on hover, mousedown, or touchstart | ||
* Programmatic background asset prefetching | ||
* Customization: disable an interaction and set a prefetch delay for hover events | ||
* Customizable | ||
* disable an interaction and set a prefetch delay for hover events | ||
* [pass a custom callback](#using-the-callback-config-property) to override the default link injection behavior | ||
* Automatically works with dynamically added links | ||
@@ -69,3 +72,4 @@ * Identify any number of containers and add more after initialization | ||
enableTouch: false, //Whether to prefetch on touchstart and therefore on mobile | ||
waitForMousedown: false //Whether to prefetch on mousedown instead of on hover | ||
waitForMousedown: false, //Whether to prefetch on mousedown instead of on hover | ||
callback: undefined //A function that overrides the default link injection behavior | ||
}); | ||
@@ -97,1 +101,21 @@ ``` | ||
* exclusions: array of partial URL strings | ||
# Using the `callback` config property | ||
By default, `prefetch` injects a `<link rel="prefetch" href="..."/>` tag in the document's head, but there may be times when it's useful to override that behavior. You can do so by adding a `callback` property to the `config` object you pass to `prefetch.init(config)`. | ||
The `callback` param must be a function and it accepts up to three parameters. In the exampmle below, the callback function evaluates the href's URL and, based on the presence of some identifier, decides whether to execute an ajax call or procede with the normal `prefetch` behavior. | ||
```javascript | ||
function handlePrefetch(url, anchor, fetch){ | ||
//url: the string from the href attribute of the anchor tag over which the user's cursor hovered | ||
//anchor: the <a> tag over which the user's cursor hovered | ||
//fetch: the function that prefetch internally executes to inject a prefetch link | ||
if(url.indexOf('some-identifier') > 0){ | ||
doAjaxCall(url); | ||
} else { | ||
fetch(url); | ||
} | ||
} | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10910
165
119