'Handle javascript iframe down case when doing postMessages

I am trying to handle iframe is down case on my react js application

I am doing postMessages to the iframe like this iframe.contentWindow.postMessage(mesage, origin);

Sometimes iframe origin can be down , with some reasons , I want to handle this part , in the console , I am get this error

Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('localhost') does not match the recipient window's origin ('null').

Question is how can I catch it ? Iframe can be load and everything can be fine , but after x minute it can be down , how to catch this error ? any ideas will be appreciated



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source