srcdoc_change_hash.html (2166B)
1 <title>same-document navigation inside an srcdoc iframe using location.hash</title> 2 <script src="/resources/testharness.js"></script> 3 <script src="/resources/testharnessreport.js"></script> 4 <script src="/resources/testdriver.js"></script> 5 <script src="/resources/testdriver-vendor.js"></script> 6 <script> 7 promise_test(async () => { 8 // Wait until 'document' is available. 9 await new Promise(resolve => window.addEventListener('load', resolve)); 10 11 // Create an iframe, wait until is is loaded. 12 let iframe = document.createElement('iframe'); 13 await new Promise(resolve => { 14 iframe.srcdoc = "srcdoc document"; 15 iframe.onload = resolve; 16 document.body.appendChild(iframe); 17 }); 18 19 assert_equals(iframe.contentDocument.body.innerText, "srcdoc document"); 20 assert_equals(iframe.contentWindow.location.href, "about:srcdoc"); 21 22 function iframeHashChanged() { 23 return new Promise(resolve => { 24 iframe.contentWindow.onhashchange = resolve; 25 }) 26 } 27 28 // 1) hash = "1". 29 { 30 let hash_changed = iframeHashChanged(); 31 await test_driver.bless("hash = '1'", () => { 32 iframe.contentWindow.location.hash = "1"; 33 }); 34 await hash_changed; 35 assert_equals(iframe.contentWindow.location.href, "about:srcdoc#1"); 36 } 37 38 // 2) hash = "2". 39 { 40 let hash_changed = iframeHashChanged(); 41 await test_driver.bless("hash = '2'", () => { 42 iframe.contentWindow.location.hash = "2"; 43 }); 44 await hash_changed; 45 assert_equals(iframe.contentWindow.location.href, "about:srcdoc#2"); 46 } 47 48 // 3) history.back(). 49 { 50 let hash_changed = iframeHashChanged(); 51 await test_driver.bless("history.back()", () => { 52 history.back(); 53 }); 54 await hash_changed; 55 assert_equals(iframe.contentWindow.location.href, "about:srcdoc#1"); 56 } 57 58 // 4) history.forward(). 59 { 60 let hash_changed = iframeHashChanged(); 61 await test_driver.bless("history.forward()", () => { 62 history.forward(); 63 }); 64 await hash_changed; 65 assert_equals(iframe.contentWindow.location.href, "about:srcdoc#2"); 66 } 67 }); 68 </script>