Total Complexity | 7 |
Complexity/F | 3.5 |
Lines of Code | 31 |
Function Count | 2 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | // ref: https://web.dev/articles/browser-level-image-lazy-loading#how-do-i-handle-browsers-that-don't-yet-support-native-lazy-loading |
||
2 | if ('loading' in HTMLImageElement.prototype) { |
||
|
|||
3 | // Replace the img.src with what is in the data-src property |
||
4 | const images = document.querySelectorAll('img[loading="lazy"]'); |
||
5 | images.forEach(img => { |
||
6 | if (img.dataset.src) { |
||
7 | img.src = img.dataset.src; |
||
8 | img.removeAttribute('data-src'); |
||
9 | } |
||
10 | if (img.dataset.srcset) { |
||
11 | img.srcset = img.dataset.srcset; |
||
12 | img.removeAttribute('data-srcset'); |
||
13 | } |
||
14 | }); |
||
15 | // Replace the source.srcset with what is in the data-srcset property |
||
16 | const sources = document.querySelectorAll('source[data-srcset]') |
||
17 | sources.forEach(source => { |
||
18 | if (source.dataset.srcset) { |
||
19 | source.srcset = source.dataset.srcset; |
||
20 | source.removeAttribute('data-srcset'); |
||
21 | } |
||
22 | if (source.dataset.sizes) { |
||
23 | source.sizes = source.dataset.sizes; |
||
24 | source.removeAttribute('data-sizes'); |
||
25 | } |
||
26 | }); |
||
27 | } else { |
||
28 | // Dynamically import the LazySizes library |
||
29 | const script = document.createElement('script'); |
||
30 | script.src = '{{ scriptSrc }}'; |
||
31 | document.body.appendChild(script); |
||
32 | } |
||
33 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.