iframe-target.html (1341B)
1 <!doctype html> 2 <title>Inner document for use in iframes.sub.html test</title> 3 <script> 4 function isInView(element) { 5 let rect = element.getBoundingClientRect(); 6 return rect.top >= 0 && rect.top <= window.innerHeight 7 && rect.left >= 0 && rect.left <= window.innerWidth; 8 } 9 10 function postResult() { 11 let position = 'unknown'; 12 if (window.scrollY == 0) 13 position = 'top'; 14 else if (isInView(document.getElementById('target'))) 15 position = 'target'; 16 else if (isInView(document.getElementById('elementid'))) 17 position = 'elementid'; 18 19 let results = { 20 scrollPosition: position, 21 href: window.location.href, 22 }; 23 24 window.top.postMessage(results, "*"); 25 } 26 27 window.addEventListener('message', (e) => { 28 if (e.data == 'getResult') { 29 // Use a timeout to get results - in the elementId fallback case, the 30 // browser may retry the text fragment search a few times before giving 31 // up and trying the elementid. 32 setTimeout(postResult, 2000); 33 } else if (e.data == 'reset') { 34 window.location.hash = ''; 35 window.scrollTo(0, 0); 36 window.top.postMessage('', "*"); 37 } 38 }); 39 </script> 40 <style> 41 p { 42 margin-top: 400vh; 43 margin-bottom: 400vh; 44 } 45 </style> 46 <body> 47 <p id="target">Target Text</p> 48 <div id="elementid">DIV</div> 49 </body>