在flutter web中调用iframeElement上的postMessage



我有一个flutter web应用程序。我需要调用postMessage,由使用在iframe元素中打开的页面处理

ui.platformViewRegistry.registerViewFactory(
'hello-world-html',
(int viewId) => html.IFrameElement()
..width = '100%'
..height = '100%'
..src = widget.url
..style.border = 'none'
);

有办法做到这一点吗?

从技术上讲,PostMessage发送到主文档的窗口,而不是iframe的窗口。

在Javascript中指定iframe的窗口对象:

document.getElementById('hello-world-html').contentWindow.postMessage(.......)

或者在达特你可以做:

import 'dart:html' as html;
// Your code
html.document.getElementById.getElementById('hello-world-html').contentWindow.postMessage(.......)

这适用于具有跨域src 的iframe

import 'dart:html';
IFrameElement element = document.getElementById('iframe') as IFrameElement;
element.contentWindow?.postMessage(data.toJson(),'*');

最新更新