|
20 | 20 | self.$exclusions = config.exclusions || [];
|
21 | 21 | config.containers = config.containers || [];
|
22 | 22 | self.addContainers(config.containers);
|
| 23 | + return self; |
23 | 24 | }
|
24 | 25 |
|
25 | 26 | self.prefetch = function(a){
|
|
88 | 89 | }
|
89 | 90 |
|
90 | 91 | 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; |
96 | 95 | }
|
97 | 96 | }
|
98 | 97 | return false;
|
|
109 | 108 | return true;
|
110 | 109 | }
|
111 | 110 |
|
| 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 | + |
112 | 118 | 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){ |
118 | 123 | 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', ''); |
122 | 127 | }
|
123 | 128 | }
|
124 | 129 |
|
|
129 | 134 | }
|
130 | 135 |
|
131 | 136 | function mousedown(e){
|
132 |
| - if(self.$lastTouchTimestamp > (new Date().getTime() - 500)){ |
133 |
| - return; |
134 |
| - } |
| 137 | + if(self.$lastTouchTimestamp > (new Date().getTime() - 500)) return; |
135 | 138 | var a = getLinkTarget(e.target);
|
136 | 139 | injectPrefetchLink(a);
|
137 | 140 | }
|
138 | 141 |
|
139 | 142 | function mouseover(e){
|
140 |
| - if(self.$lastTouchTimestamp > (new Date().getTime() - 500)){ |
141 |
| - return; |
142 |
| - } |
| 143 | + if(self.$lastTouchTimestamp > (new Date().getTime() - 500)) return; |
143 | 144 | var a = getLinkTarget(e.target);
|
144 | 145 | if(a && isPrefetchable(a)){
|
145 | 146 | a.addEventListener('mouseout', mouseout);
|
|
173 | 174 | }
|
174 | 175 | }
|
175 | 176 |
|
176 |
| - return new Prefetch(); |
| 177 | + return new Prefetch().init(); |
177 | 178 | });
|
0 commit comments