How do I read a window.postMessage() from an Iframe to update an array

I have no idea as to achieve this…I’m pretty sure I can’t interrogate the post message with a query manager in wappler.

What would be the easiest way to update an array in the parent page from an iframe event.
If you can a bit of pseudo code would really help…

Thanks :slight_smile:

A window.postMessage() will trigger a message event on that window, so you need to listen there for the message window.onMessage = function() {} or window.addEventListener('message', function() {}).

Hi Patrick,
Great thankyou…
how would I update the array ‘content’ with the function,
unsure how to use dmx with js

window.addEventListener(‘message’, function() {

content = message ???
}) .

I have previously instantiated the variable

use dmx.parse('content.setValue("' + message + '")');

1 Like

Be careful naming the array ‘content’. If using NodeJS the area used to add content pages has an ID of content so you could easily end up with clashed ID. Even if you aren’t using Node now, I’d recommend using something else in case you transfer this project to NodeJS in the future.

Thanks bpj for the great advice,
As it happens I am using node so I’m sure you have helped me avoid potential problems…

1 Like

This works except for the dmx part…Any ideas??

var eventMethod = window.addEventListener ? "addEventListener": "attachEvent";
var eventer = window[eventMethod];
var messageEvent = eventMethod === "attachEvent" ? "onmessage": "message";

    eventer(messageEvent, function (e){
        var message = || e.message;

        if(message === "myevent"){
        dmx.parse('session_status.setValue("' + message + '")')

Ok sorted
I placed the variable session_status in the layout page not the page where the iFrame was located…