file_scrollRestoration_bfcache_and_nobfcache_part2.html (1160B)
1 <html> 2 <head> 3 <script> 4 // Note, this page does not enter bfcache because of an HTTP header. 5 6 // Ensure layout is flushed before doing anything with scrolling. 7 function flushAndExecute(callback) { 8 window.requestAnimationFrame(function() { 9 setTimeout(callback); 10 }); 11 } 12 13 var bc = new BroadcastChannel("notbfcached"); 14 bc.onmessage = (msgEvent) => { 15 if (msgEvent.data == "scroll") { 16 flushAndExecute(() => { window.scrollTo(0, 4000); }); 17 } else if (msgEvent.data == "getScrollY") { 18 flushAndExecute(() => { bc.postMessage({ scrollY: window.scrollY}); }); 19 } else if (msgEvent.data == "back") { 20 flushAndExecute(() => { bc.close(); history.back(); }); 21 } else if (msgEvent.data == "close") { 22 bc.postMessage("closed"); 23 bc.close(); 24 window.close(); 25 } 26 }; 27 window.onpageshow = (event) => { 28 bc.postMessage({command: "pageshow", persisted: event.persisted}); 29 }; 30 </script> 31 </head> 32 <body> 33 <div style="height: 5000px; border: 1px solid black;">content</div> 34 </body> 35 </html>