Template: WhatLinksHere: Difference between revisions
From ASDSO Dam Safety Toolbox
No edit summary |
No edit summary |
||
Line 37: | Line 37: | ||
document.querySelector("iframe").addEventListener("click", function(e){ | document.querySelector("iframe").addEventListener("click", function(e){ | ||
var iframeWin = document.getElementById("iframeWin"); | |||
iframeWin.contentWindow.postMessage(null, 'https://your-second-site.example'); | |||
}); | |||
window.addEventListener('message', event => { | |||
console.log(event); | |||
console.log(event.origin); | |||
// IMPORTANT: check the origin of the data! | |||
if (event.origin === 'https://your-first-site.example') { | |||
// The data was sent from your site. | |||
// Data sent with postMessage is stored in event.data: | |||
console.log(event.data); | |||
} else { | |||
// The data was NOT sent from your site! | |||
// Be careful! Do not use it. This else branch is | |||
// here just for clarity, you usually shouldn't need it. | |||
return; | |||
} | |||
}); | }); | ||
</script> | </script> | ||
</html> | </html> |
Revision as of 14:03, 7 July 2023
Usage