Skip to content

Commit 4449276

Browse files
author
Joe Podwys
committed
Merge pull request #1 from jpodwys/dev
Fixing two bugs
2 parents ba39c6e + fa19c8b commit 4449276

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "prefetch",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "Customizable interaction-based link prefetching.",
55
"main": "prefetch.js",
66
"repository": {

prefetch.js

+21-20
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
self.$exclusions = config.exclusions || [];
2121
config.containers = config.containers || [];
2222
self.addContainers(config.containers);
23+
return self;
2324
}
2425

2526
self.prefetch = function(a){
@@ -88,11 +89,9 @@
8889
}
8990

9091
function isExcluded(a){
91-
if(self.$exclusions.length){
92-
for(var i = 0; i < self.$exclusions.length; ++i){
93-
if(a.href.indexOf(self.$exclusions[i]) > -1){
94-
return true;
95-
}
92+
for(var i = 0; i < self.$exclusions.length; ++i){
93+
if(a.href.indexOf(self.$exclusions[i]) > -1){
94+
return true;
9695
}
9796
}
9897
return false;
@@ -109,16 +108,22 @@
109108
return true;
110109
}
111110

111+
function createLinkTag(url){
112+
var link = document.createElement('link');
113+
link.setAttribute('rel', 'prefetch');
114+
link.setAttribute('href', url);
115+
return link;
116+
}
117+
112118
function injectPrefetchLink(a){
113-
if(a && isPrefetchable(a)){
114-
var url = (typeof a === 'object') ? a.href : a;
115-
var link = document.createElement('link');
116-
link.setAttribute('rel', 'prefetch');
117-
link.setAttribute('href', url);
119+
if(!a) return;
120+
var url = (typeof a === 'object') ? a.href : a;
121+
var link = (url) ? createLinkTag(url) : null;
122+
if(link){
118123
document.getElementsByTagName('head')[0].appendChild(link);
119-
if(typeof a === 'object'){
120-
a.setAttribute('data-no-prefetch', '');
121-
}
124+
}
125+
if(typeof a === 'object'){
126+
a.setAttribute('data-no-prefetch', '');
122127
}
123128
}
124129

@@ -129,17 +134,13 @@
129134
}
130135

131136
function mousedown(e){
132-
if(self.$lastTouchTimestamp > (new Date().getTime() - 500)){
133-
return;
134-
}
137+
if(self.$lastTouchTimestamp > (new Date().getTime() - 500)) return;
135138
var a = getLinkTarget(e.target);
136139
injectPrefetchLink(a);
137140
}
138141

139142
function mouseover(e){
140-
if(self.$lastTouchTimestamp > (new Date().getTime() - 500)){
141-
return;
142-
}
143+
if(self.$lastTouchTimestamp > (new Date().getTime() - 500)) return;
143144
var a = getLinkTarget(e.target);
144145
if(a && isPrefetchable(a)){
145146
a.addEventListener('mouseout', mouseout);
@@ -173,5 +174,5 @@
173174
}
174175
}
175176

176-
return new Prefetch();
177+
return new Prefetch().init();
177178
});

0 commit comments

Comments
 (0)