hasfocus-same-site-outer.html (1535B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>hasFocus() outer</title> 6 <script> 7 var w = null; 8 var hadInitialFocus = false; 9 var hadLostFocus = false; 10 window.onmessage = function(e) { 11 if (e.data == "restored") { 12 if (hadInitialFocus && hadLostFocus) { 13 let verdict = document.hasFocus() ? "PASS" : "FAIL: hasFocus false"; 14 window.opener.postMessage(verdict, "*"); 15 } else { 16 window.opener.postMessage("FAIL: should have failed already", "*"); 17 } 18 } else if (e.data == "focused") { 19 w = window.open("hasfocus-other.html"); 20 } else if (e.data == "noblur") { 21 window.opener.postMessage("FAIL: no inner blur", "*"); 22 } else if (e.data == "opened") { 23 if (document.hasFocus()) { 24 w.close(); 25 window.opener.postMessage("FAIL: focus not lost when other window opened", "*"); 26 } else { 27 hadLostFocus = true; 28 window.frames[0].postMessage("hasfocus", "*"); 29 } 30 } else if (e.data == "close") { 31 w.close(); 32 } else if (e.data == "wrongfocus") { 33 w.close(); 34 window.opener.postMessage("FAIL: hasFocus was true in iframe", "*"); 35 } 36 } 37 window.onload = function() { 38 if (document.hasFocus()) { 39 hadInitialFocus = true; 40 window.frames[0].postMessage("focus", "*"); 41 } else { 42 window.opener.postMessage("FAIL: did not have initial focus", "*"); 43 } 44 } 45 </script> 46 </head> 47 <body> 48 <iframe src="hasfocus-inner.html"></iframe> 49 </body> 50 </html>