Adding Unique HTML Widgets to Dynamic Page

Alexander, I just happened to stumble across this thread. You state:

A small note about posting messages to IFrame :
For now, due to asynchronous nature of loading of the IFrame component, it is possible that the code in the onReady section will run before the IFrame loaded - meaning that the message would be sent before the IFrame is able to get it (so it will be lost).
The basic workaround in this case will be to send the message after a small delay:
instead of $w(" #html2 “).postMessage(“Message from page code!”); it would be setTimeout(() => $w(” #html2 ").postMessage(“Message from page code!”), 2000).

If you look closely, Beverly used a piece of code that I published on this forum a long, long time ago, as a solution to the problem that the page loads first, but the html-component isn´t ready yet. I re-published it on my blog here : https://girizano.wixsite.com/codecorner/home/html-component-not-running-in-publish-mode-google-sheets-example
Basically, the Wix Page waits for a “heart beat” from the component BEFORE it sends the content with postMessage. You offer an alternative, using a timeout (2000 ms). I am not a great proponent of relying on time-outs, since it relies on guessing (and in my case, on the slower side of the Internet world, 2 secs isn´t even enough sometimes). But, everybody if free to implement any solution that they seem fit.
But there is another problem: when the html-component loads BEFORE the Wix Page. I ran into this behaviour when developing my alternative date picker (https://girizano.wixsite.com/codecorner/home/date-picker-in-another-language). Suddenly, the component started to load consistently before the page, or in other words, the Wix Page loaded after the component.
Then this would happen:

  • component loads, send heart beat message
  • Page not ready yet, message goes into deep space
  • result: html-component stays empty

I have discussed this with some of you, do not remember who (it´s still here on this forum), but the only workaround that I found to work reliable in BOTH cases (page loads before component/component loads before page) was to throw it twice: at the top of your page, you send the info to the component and you ALSO wait for a heart beat, as earlier described.

This means:

  1. if component is loaded before page, it sends the heart beat, page not ready, so ignored. When page is ready, data is received anyway because it is sent from the beginning of the page
  2. if component is loaded after page: first message from Wix Page to component is sent, but goes into deep space because component has not loaded yet. When component IS loaded, sends heart beat, is received by page and message is sent AGAIN, html-component receives data, all well.

So, worst case, you send it twice. Just wanted to let you know about these problems and solutions and, if you have a better alternative, or if I am mistaken somewhere, please let me know.