2

I have a Web based Training where we'd like to embed an MS-Form as an Iframe into a page and via Javascript would like to enable the next page when the user has submitted the form.

Is there any way possible to check if the MS-Form inside the iframe was submitted?

I tried by getting the iframe via an assigned ID, but it is not possible to access the iframes #document (security cross-origin reason) and for example check buttons.

Maybe there are some messages that can be posted to the contentWindow?

I've also seen, that there is no API that would support checking if I've already submitted a certain form. As far as I have seen it is only possible to access your own forms via API.

Brunonen
  • 21
  • 3

1 Answers1

2

You can use the PostMessage api to listen/send events from and to the iframe. https://learn.microsoft.com/en-us/microsoft-365/cloud-storage-partner-program/online/scenarios/postmessage

You can listen to events by adding this event listener after the iframe has loaded:

function handlePostMessage(e) {
   // The actual message is contained in the data property of the event.
    var msg = JSON.parse(e.data);

    // The message ID is now a property of the message object.
    var msgId = msg.MessageId;

    // The message parameters themselves are in the Values
    // parameter on the message object.
    var msgData = msg.Values;

    // Do something with the message here. }

window.addEventListener('message', handlePostMessage, false);
Joost00719
  • 716
  • 1
  • 5
  • 20
  • Thanks for the answer. I've added the code, but the Iframe doesn't seem to send any messages to the my parent page. I've tried posting a message to the iframe with the methods mentioned in the link you've provided, but none of them seem to give me a reply. Is there a working demo of this and one of the functions? – Brunonen Oct 26 '22 at 08:09
  • 1
    It could be very well possible that the page you're trying to interact with does not implement any PostMessage communication. In that case, you're out of luck :/ – Joost00719 Oct 26 '22 at 10:03