|
1 | 1 | import loadingAttributePolyfill from '../dist/loading-attribute-polyfill.module.js'; |
2 | 2 |
|
3 | 3 | // Test for dynamically inserted images |
4 | | -window.setTimeout(() => { |
5 | | - let child = document.createElement('div'); |
6 | | - child.innerHTML = '<noscript class="loading-lazy"></noscript>'; |
7 | | - child = child.firstChild; |
| 4 | +let addImage = (event) => { |
| 5 | + let noscriptElement = document.createElement('noscript'), |
| 6 | + imageElement = document.createElement('img'); |
| 7 | + |
| 8 | + noscriptElement.classList.add('loading-lazy'); |
8 | 9 |
|
9 | | - let imageElement = document.createElement('img'); |
10 | 10 | imageElement.setAttribute('src', 'https://via.placeholder.com/250x150'); |
11 | 11 | imageElement.setAttribute('loading', 'lazy'); |
12 | 12 | imageElement.setAttribute('alt', '..'); |
13 | 13 | imageElement.setAttribute('width', '250'); |
14 | 14 | imageElement.setAttribute('height', '150'); |
15 | | - child.appendChild(imageElement); |
| 15 | + |
| 16 | + noscriptElement.appendChild(imageElement); |
| 17 | + |
16 | 18 | document |
17 | | - .getElementsByTagName('main')[0] |
18 | | - .insertBefore(child, document.getElementsByTagName('main')[0].firstChild); |
| 19 | + .querySelector('main') |
| 20 | + .insertAdjacentElement('beforeend', noscriptElement); |
19 | 21 |
|
20 | 22 | loadingAttributePolyfill.prepareElement( |
21 | 23 | document.querySelector('main noscript.loading-lazy') |
22 | 24 | ); |
23 | | -}, 5000); |
| 25 | + |
| 26 | + event.preventDefault(); |
| 27 | +}; |
| 28 | + |
| 29 | +document.querySelector('button.add-image').addEventListener('click', addImage); |
0 commit comments