window.postmessage在不安置的情况下无法工作

发布于 2025-02-13 07:30:35 字数 1636 浏览 0 评论 0原文

我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深者入戏 2025-02-20 07:30:35

如@Kaiido所述,iframe的Onload功能确实有困难。我的反应代码的一部分看起来如下:

<iframe  ref={elemRef} src='https://d34gxw3jqljnhg.cloudfront.net/sample3.html' frameBorder="0"></iframe>
 
const elemRef = useCallback((node) => {
    if (node !== null) {
        node.onload = () => node?.contentWindow?.postMessage({call:'sendValue', value: {task: {input: taskInput}}}, 'https://d3hs63jqljnhg.cloudfront.net/sample3.html');
    }
}, [])

As mentioned by @kaiido, iFrame's onload function did trick. My part part of the React code looks like below:

<iframe  ref={elemRef} src='https://d34gxw3jqljnhg.cloudfront.net/sample3.html' frameBorder="0"></iframe>
 
const elemRef = useCallback((node) => {
    if (node !== null) {
        node.onload = () => node?.contentWindow?.postMessage({call:'sendValue', value: {task: {input: taskInput}}}, 'https://d3hs63jqljnhg.cloudfront.net/sample3.html');
    }
}, [])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文