Source code
Revision control
Copy as Markdown
Other Tools
Test Info:
- This WPT test may be referenced by the following Test IDs:
- /html/semantics/embedded-content/the-iframe-element/cross-site/iframe-loading-lazy-cross-site.sub.html - WPT Dashboard Interop Dashboard
<!DOCTYPE html>
<head>
<title>Iframes with loading='lazy' load when in the viewport</title>
<link rel="author" title="Scott Little" href="mailto:sclittle@chromium.org">
<link rel="author" title="Dom Farolino" href="mailto:dom@chromium.org">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<script>
const t_in_viewport =
async_test('In-viewport iframes load eagerly');
const t_below_viewport =
async_test('Below-viewport iframes load lazily');
const expected_url = "http://{{hosts[alt][]}}:{{ports[http][0]}}/html/semantics/embedded-content/the-iframe-element/cross-site/resources/subframe-post-msg.html?below-viewport";
let has_window_loaded = false;
const in_viewport_iframe_onload = t_in_viewport.step_func_done(() => {
assert_false(has_window_loaded,
"The in_viewport iframe should not block the load event");
});
window.addEventListener("load", () => {
has_window_loaded = true;
document.getElementById("below_viewport").scrollIntoView();
});
window.addEventListener("message", t_below_viewport.step_func_done(event => {
assert_equals(event.data, expected_url,
"The below-viewport iframe should have loaded its cross-site document");
assert_true(has_window_loaded,
"The window load event should have fired before the below-viewport iframe loads");
}));
</script>
<body>
<iframe id="in_viewport"
loading="lazy" width="200px" height="100px"
onload="in_viewport_iframe_onload();"></iframe>
<div style="height:2000vh;"></div>
<iframe id="below_viewport"
loading="lazy" width="200px" height="100px"></iframe>
<!-- This async script loads very slowly in order to ensure that, if the
below_viewport* elements have started loading, it has a chance to finish
loading before window load event fires, so that the test will dependably
fail in that case instead of potentially passing depending on how long
different resource fetches take. -->
<script async src="/common/slow.py"></script>
</body>