|
37 | 37 | |--------------------------------------------------------------------------
|
38 | 38 | */
|
39 | 39 |
|
40 |
| - // Backup jQuery.ready |
41 |
| - var _jqueryReady; |
42 |
| - |
43 | 40 | // Common texts
|
44 | 41 | var _dataLayer = 'dataLayer';
|
45 |
| - var _deferClass = 'deferjs'; |
46 |
| - var _deferPrefix = 'defer-'; |
47 |
| - var _lazied = 'lazied'; |
48 |
| - var _dataPrefix = 'data-'; |
49 |
| - var _media = 'media'; |
50 |
| - |
51 |
| - // Common class names |
52 |
| - var _classLazied = _deferPrefix + _lazied; |
53 |
| - var _classLoaded = _deferPrefix + 'loaded'; |
54 |
| - var _classLoading = _deferPrefix + 'loading'; |
55 |
| - |
56 |
| - // Common attributes |
57 |
| - var _attrClassName = 'className'; |
58 |
| - var _attrDataIgnore = 'data-ignore'; |
59 | 42 |
|
60 | 43 | // Common CSS selectors
|
61 |
| - var _queryTarget = '.' + _classLoading + ':not([' + _attrDataIgnore + ']):not([lazied])'; |
| 44 | + var _queryTarget = '.defer-loading:not([data-ignore]):not([lazied])'; |
62 | 45 |
|
63 | 46 | /*
|
64 | 47 | |--------------------------------------------------------------------------
|
|
77 | 60 | */
|
78 | 61 |
|
79 | 62 | function _replaceClass(node, find, replace) {
|
80 |
| - node[_attrClassName] = (' ' + node[_attrClassName] + ' '). |
81 |
| - replace(' ' + find + ' ', ' ' + replace + ' ').trim(); |
| 63 | + node.className = ((' ' + node.className + ' '). |
| 64 | + replace(' ' + find + ' ', ' ') + replace).trim(); |
82 | 65 | }
|
83 | 66 |
|
84 | 67 | function _lazyload() {
|
85 |
| - defer.dom(_queryTarget, 0, _classLazied, function (node) { |
86 |
| - _replaceClass(node, _classLoading, _classLoaded); |
| 68 | + defer.dom(_queryTarget, 0, '', function (node) { |
| 69 | + _replaceClass(node, 'defer-loading', 'defer-loaded'); |
87 | 70 | }, _options);
|
88 | 71 |
|
89 | 72 | [].slice.call(document.querySelectorAll('style[defer]')).
|
90 | 73 | forEach(function(node) {
|
91 |
| - node[_media] = node.getAttribute(_dataPrefix + _media) || 'all'; |
| 74 | + node.media = node.getAttribute('data-media') || 'all'; |
92 | 75 | });
|
93 | 76 | }
|
94 | 77 |
|
|
102 | 85 | }
|
103 | 86 | }
|
104 | 87 |
|
105 |
| - function _boot() { |
106 |
| - defer(_lazyload, _delay); |
107 |
| - _replaceClass(document.documentElement, 'no-' + _deferClass, _deferClass); |
108 |
| - _copyright(); |
109 |
| - } |
110 |
| - |
111 | 88 | /*
|
112 | 89 | |--------------------------------------------------------------------------
|
113 | 90 | | Define helper object
|
114 | 91 | |--------------------------------------------------------------------------
|
115 | 92 | */
|
116 | 93 |
|
| 94 | + _replaceClass( |
| 95 | + document.documentElement, |
| 96 | + 'no-deferjs', |
| 97 | + defer ? 'deferjs' : '' |
| 98 | + ); |
| 99 | + |
117 | 100 | // Check if missing defer feature
|
118 |
| - if (!defer) {return;} |
| 101 | + if (!defer) { |
| 102 | + return; |
| 103 | + } |
119 | 104 |
|
120 | 105 | // Fallback for older versions
|
121 | 106 | window.defer_helper = {'defermedia': _lazyload};
|
|
131 | 116 | window.ga = window.ga || function () {(window.ga.q = window.ga.q || []).push(arguments)}; window.ga.l = Number(Date());
|
132 | 117 | window[_dataLayer] = window[_dataLayer] || [];
|
133 | 118 |
|
134 |
| - // Fake jQuery.ready, if jQuery loaded |
135 |
| - defer(function (jquery) { |
136 |
| - if (_jqueryReady) { |
137 |
| - return; |
138 |
| - } |
139 |
| - |
140 |
| - jquery = window.jQuery; |
141 |
| - |
142 |
| - if (jquery && jquery.fn) { |
143 |
| - _jqueryReady = jquery.fn.ready; |
144 |
| - jquery.fn.ready = function (callback) { |
145 |
| - defer(function () {_jqueryReady(callback)}, _delay); |
146 |
| - } |
147 |
| - } |
148 |
| - }); |
149 |
| - |
150 | 119 | /*
|
151 | 120 | |--------------------------------------------------------------------------
|
152 | 121 | | Main
|
153 | 122 | |--------------------------------------------------------------------------
|
154 | 123 | */
|
155 | 124 |
|
156 |
| - |
157 |
| - _boot(); |
| 125 | + _copyright(); |
| 126 | + defer(_lazyload, _delay); |
158 | 127 |
|
159 | 128 | })(this, document, console);
|
0 commit comments