首页 > 解决方案 > iframe 上的 postMessage 调用序列

问题描述

在我的主文档中,我有函数 showOnMap,它通过几个 postMessage 方法与内部 iframe 进行通信。然后我被要求在第一个完成后才调用第二个 postMessage。是否有可能做到这一点?我无法控制 iframe 本身,它来自外部来源。我当前的代码:

function showOnMap(record: ITableData) {
  const frame = document.querySelector('iframe') as HTMLIFrameElement;
  if (frame && frame.contentWindow) {
    frame.contentWindow.postMessage(getFirstMessage(record), '*');
    frame.contentWindow.postMessage(getSecondMessage([record]), '*');
  }
}

标签: javascripttypescriptasynchronousiframe

解决方案


推荐阅读