Question

Passing variable from unbounce landing page to iframe popup?


Badge

I have an unbounce landing page that I am trying to pass a variable that is stored in SessionStorage to the native unbounce popup. Since the popup shows as an iFrame, I am trying to use postMessage in JavaScript to send that variable but it doesn’t seem to be working for me. Has anyone successfully managed to do this?

To clarify, I have seen a bunch of stuff sending variables from the popup to the parent page but not the other way around. Any help would be much appreciated!

Thank you!


2 replies

Userlevel 7
Badge +3

Hi @gr8hoops21

What are the 2 scripts you are using? 

 

Badge

Hi @Hristian,

I think I am doing it backwards right now but here is the code:

Landing Page JS:

<script>  
// This will need to be specific to the URL for your popup
const popupOrigin = "http://ce151c01c4a64ba9b3b84af944e85d0a.pages.ubembed.com/";

// On receiving message from the Convertable set a cookie
window.onload = function () {
function handleMessage (e) {
var eventData = JSON.stringify(e.data);

if (e.origin.includes(popupOrigin)) {
// Check for the message
if (eventData === 'variable') {
// do whatever you need with the variable
console.log('Test');
}
}
}

// Listen for the message from the popup
window.addEventListener('message', handleMessage);
}
</script>

iFrame Popup JS:

<script>
const buttonId = "#lp-pom-button-51";
const hostDomain = "*";

document.querySelector(buttonId).onclick = function () {
window.top.postMessage(JSON.stringify("variable"), hostDomain);
};
</script>

I have a button on the iFrame popup with that ID.

 

Could it potentially have any thing to do with the popup not showing up until 3 seconds after the page loads?

Reply