iframe-loading-lazy-multiple-times.html (2470B)
1 <!DOCTYPE html> 2 <head> 3 <title>Iframes with loading='lazy' can be lazy loaded multiple times</title> 4 <link rel="author" title="Dom Farolino" href="mailto:dom@chromium.org"> 5 <link rel="help" href="https://html.spec.whatwg.org/multipage/urls-and-fetching.html#lazy-loading-attributes"> 6 <script src="/resources/testharness.js"></script> 7 <script src="/resources/testharnessreport.js"></script> 8 </head> 9 10 <body> 11 <!-- This is used to represent the top of the viewport, so we can scroll the 12 below-viewport iframe out-of-view later in the test --> 13 <div id="top_div"></div> 14 <div style="height:1000vh;"></div> 15 <iframe id="below_viewport" loading="lazy" src="resources/unload-reporter.html?first"></iframe> 16 17 <script> 18 const t = async_test(); 19 const iframe = document.querySelector('#below_viewport'); 20 const top_div = document.querySelector('#top_div'); 21 22 let has_window_load_fired = false; 23 let iframe_being_unloaded = false; 24 25 // This should be triggered first. 26 window.addEventListener('load', t.step_func(() => { 27 has_window_load_fired = true; 28 // Scroll the loading=lazy below-viewport iframe into view, so that it loads. 29 iframe.scrollIntoView(); 30 })); 31 32 window.addEventListener('message', t.step_func(msg => { 33 if (msg.data === 'unloading') 34 iframe_being_unloaded = true; 35 })); 36 37 iframe.onload = t.step_func(() => { 38 assert_true(has_window_load_fired, 39 "The loading=lazy below-viewport iframe should not block the " + 40 "window load event"); 41 changeIframeSourceAndScrollToTop(); 42 }); 43 44 function changeIframeSourceAndScrollToTop() { 45 top_div.scrollIntoView(); 46 47 // Lazily load a "different" iframe. 48 iframe.src = 'resources/unload-reporter.html?second'; 49 iframe.onload = 50 t.unreached_func("The loading=lazy below-viewport iframe should lazily " + 51 "load its second resource, and not load it eagerly " + 52 "when the `src` attribute is changed"); 53 54 // In 1s, scroll the iframe *back* into view, and record that it loads 55 // successfully. 56 t.step_timeout(() => { 57 assert_false(iframe_being_unloaded, 58 "The iframe's old resource is not eagerly unloaded"); 59 60 iframe.onload = t.step_func_done(() => { 61 assert_true(iframe_being_unloaded, 62 "The iframe's old resources was unloaded correctly"); 63 }); 64 65 iframe.scrollIntoView(); 66 }, 1000); 67 } 68 </script> 69 </body>