sessionStorage-about-blank-partitioned-iframe.html (1244B)
1 <!doctype html> 2 <meta charset="utf-8"> 3 <script> 4 5 function getOrCreateID(key) { 6 if (!sessionStorage.getItem(key)) { 7 const newID = new Date() + "-" + Math.random(); 8 sessionStorage.setItem(key, newID); 9 } 10 return sessionStorage.getItem(key); 11 } 12 13 window.addEventListener("load", () => { 14 // In this testing set-up, only cross-site iframes will have an opener. 15 if (parent.opener) { 16 const payload = { 17 message: "cross-site window iframe loaded", 18 userID: getOrCreateID("userID"), 19 } 20 // Once the cross-site iframe has loaded, we send a message back to 21 // the main window with the ID from sessionStorage. 22 parent.opener.postMessage(payload, parent.opener.origin); 23 } 24 }); 25 26 window.addEventListener("message", (e) => { 27 if (e.data.command == "create ID") { 28 // e.data.key is equivalent to "userID" 29 getOrCreateID(e.data.key); 30 31 const payload = { 32 message: "ID created", 33 userID: sessionStorage.getItem("userID"), 34 } 35 // Return the ID from sessionStorage to the main window. 36 e.source.postMessage(payload, e.source.origin); 37 } 38 39 // Additional functionality for clean-up at the end of the test. 40 if (e.data.command == "clearStorage") { 41 sessionStorage.clear(); 42 } 43 }); 44 </script>