window.postmessage在不安置的情况下无法工作
我有iframe在通过JS Postmessage API接收数据后加载CDN模板,
<iframe ref={elemRef} style={{position: 'relative', height: '90vh', width: '100%'}} id={'myIframe'} src='https://d3hs63jqljnhg.cloudfront.net/sample3.html' frameBorder="0"></iframe>
const elemRef = useCallback((node) => {
if (node !== null) {
setTimeout(() => {
node.contentWindow.postMessage({call:'sendValue', value: {task: {input: taskInput}}});
}, 500);
}
如果我删除settimeout
,它不会发布任何消息。我想知道为什么在React上,除非与SettiMeT一起使用,否则window.postmessage()不起作用。控制台中根本没有错误,但必须延迟约500个才能工作。
我还尝试了使用postmessage
使用效果中的API,但是当它控制时,我仍然可以看到框架,但是我看不到postmessage
被触发的
const isIFrame = (input: HTMLElement | null): input is HTMLIFrameElement =>
input !== null && input.tagName === 'IFRAME';
useEffect(() => {
const iFrame = document.getElementById('myFrame')
console.log("iframeeeee",iFrame)
if(isIFrame(iFrame) && iFrame && iFrame.contentWindow){
iFrame.contentWindow.postMessage({call:'sendValue', value: {task: {input: taskInput}}}, 'https://d3hs63jqljnhg.cloudfront.net/sample3.html');
}
})
useEffect(() => {
const iFrame = document.getElementById('myFrame')
console.log("iframeeeee",iFrame)
if(isIFrame(iFrame) && iFrame && iFrame.contentWindow){
iFrame.contentWindow.postMessage({call:'sendValue', value: {task: {input: taskInput}}}, 'https://d3hs63jqljnhg.cloudfront.net/sample3.html');
}
}, [])
人可以解释一下吗?我更喜欢避免使用Settimeout,因为感觉不可靠。
I have iFrame which loads CDN template after it receives data through JS PostMessage API
<iframe ref={elemRef} style={{position: 'relative', height: '90vh', width: '100%'}} id={'myIframe'} src='https://d3hs63jqljnhg.cloudfront.net/sample3.html' frameBorder="0"></iframe>
const elemRef = useCallback((node) => {
if (node !== null) {
setTimeout(() => {
node.contentWindow.postMessage({call:'sendValue', value: {task: {input: taskInput}}});
}, 500);
}
If I remove setTimeOut
, it doesn't post any message. I wonder why on the react, the window.postMessage() doesn't work unless it's used with setTimeout. There's no error at all in the console, but it has to be delayed for about 500 in order to work.
I also tried using postMessage
API in useEffects but still I can see the frame when it console Log but I don't see postMessage
being triggered
const isIFrame = (input: HTMLElement | null): input is HTMLIFrameElement =>
input !== null && input.tagName === 'IFRAME';
useEffect(() => {
const iFrame = document.getElementById('myFrame')
console.log("iframeeeee",iFrame)
if(isIFrame(iFrame) && iFrame && iFrame.contentWindow){
iFrame.contentWindow.postMessage({call:'sendValue', value: {task: {input: taskInput}}}, 'https://d3hs63jqljnhg.cloudfront.net/sample3.html');
}
})
useEffect(() => {
const iFrame = document.getElementById('myFrame')
console.log("iframeeeee",iFrame)
if(isIFrame(iFrame) && iFrame && iFrame.contentWindow){
iFrame.contentWindow.postMessage({call:'sendValue', value: {task: {input: taskInput}}}, 'https://d3hs63jqljnhg.cloudfront.net/sample3.html');
}
}, [])
Can anyone explain that? I prefer avoiding setTimeout because it feels unreliable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如@Kaiido所述,iframe的Onload功能确实有困难。我的反应代码的一部分看起来如下:
As mentioned by @kaiido, iFrame's onload function did trick. My part part of the React code looks like below: