I have a scenario where an iframe is used to receive information through the contentWindow.postMessage function in order to log in to a page that I am creating. However, I am facing an issue where the page loads before the contentWindow.postMessage message is received. How can I ensure that the page loads only after the domain has received the necessary information?
Below is the code snippet:
Page containing the iframe:
<template>
<iframe
id="iframe"
name="iframe"
src="http://localhost:8081/login"
frameborder="0" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;height:100%;width:100%;position:absolute;top:0px;left:0px;right:0px;bottom:0px" height="100%" width="100%"
>
></iframe>
</template>
<script>
let show = true;
export default {
name: 'appFrame',
function(){ document.getElementById('iframe').contentWindow.postMessage({
userData: {
info1: localStorage.info1,
info2: localStorage.info2,
info3: localStorage.info3
}},
'http://localhost:8081/list');
},
data () {
return {
//
}
}
}
</script>
<style scoped>
</style>
Here is the window.onmessage code snippet on the page where I want to load the information:
window.onmessage = function(event)
{
if(event.origin == 'http://localhost:8080')
document.getElementById('output').innerHTML = event.data;
localStorage.setItem("info1", event.data.userData.info1);
localStorage.setItem("info2", event.data.userData.info2);
localStorage.setItem("info3", event.data.userData.info3);
console.log("LOCALSTORAGE: ", localStorage.getItem("info1"), localStorage.getItem("info2"), localStorage.getItem("info3") )
};
What would be the best approach to address this issue?