postMessage - passing/receiving an array

Hello, all,

I know that window.postMessage can pass more than just a string. You can pass objects, arrays, etc.

My question is: if an array is passed, will event.data automatically become the array? Or do I need to do something else in order to make the array useable on the receiving end?

V/r,

:slight_smile:

Never mind… I think I fingered it out.

Parent page loads iFrame that contains child page.

After iFrame, include JavaScript that will contain a function that will JSON.parse() a response. Follow that with a conditional to check if browser supports addEventListener() - if not, then set attachEvent() - for “onmessage” that will run said function.

In child page, set a variable to parent, then create a blank array. Use .push() to store as many elements as needed into the array. Stringify the array into a JSON object and parent.postMessage(); to send message back to parent.

Upon sending a message, parent page will parse the data and VOILA!

The downside to this is that I can’t test if this will cross from HTTP to HTTPS until after it’s on production. Joy.

HTH,

:slight_smile:

Och… I just thought of something.

When a parent document is on HTTP, and contains an iFrame which has a source in HTTPS, will using parent in the iFrame document reference the parent document? Or will that violate Same-Origin policy?

V/r,

:slight_smile:

According to this MDN article, the iFrame document can read parent, but not write to parent.

Network Admins will not allow me to set our servers to work with CORS.

Unless someone has a better suggestion, it’s looking more and more like I’ll have to load the whole section into HTTPS, then make sure that any other pages are set to HTTP. That’s going to be more overhead.

I am simply trying to make a form submit via AJaX so it’s in the background, and use the DOM to display validation / error / confirmation messages. I can use window.postMessage() to initiate contact from parent document to iframe document, but getting the messages back to the parent document is escaping me.

Serious suggestions appreciated.

V/r,

:slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.