You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Appscript WebApp的iframe跨窗口发消息刷新异常问题

iframe跨域消息传递刷新失效及手动刷新空白问题

主HTML页面包含iframe1和iframe2,两者均为关联Google Sheets的Appscript WebApp。期望点击iframe1中的按钮后,通过postMessage触发iframe2自行刷新,但控制台显示消息已成功接收,iframe2却未完成刷新;手动右键刷新iframe2时,页面会变为空白。

主HTML代码

<script>
    // Function to refresh the second iframe
    function refreshSecondIframe() {
        var iframe2 = document.getElementById('iframe2');
        console.log("Sending message to iframe2");
        iframe2.contentWindow.postMessage('refreshIframe', 'https://example.com');  // Update the domain to match your actual domain
    }

    // Event listener to handle postMessage from iframe1
    window.addEventListener('message', function (event) {
        console.log("Received message in main HTML document:", event);

        // Check data before taking action
        if (event.data === 'buscarButtonClicked') {
            refreshSecondIframe();
        }
    });
</script>

iframe1 HTML代码

<input type="button" value="Buscar" onclick="submitFormAndNotifyParent()" style="grid-column: span 2; margin-top: 10px;">

<script>
    function submitFormAndNotifyParent() {
        if (window.parent !== window.top) {
            console.log("Sending message from iframe1 to parent window");
            window.top.postMessage("buscarButtonClicked", 'https://example.com');
        }
        submitForm() 
    }
</script>

iframe2 HTML代码

<script>
    window.addEventListener('message', function (event) {
        console.log("Received message in iframe2:", event);

        // Check data before taking action
        if (event.data === 'refreshIframe') {
            console.log("Refreshing iframe2");

            // Use the following line to forcefully reload the iframe
            document.location.href = document.location.href;
        }
    });
</script>

运行该功能的站点为 www.example.com/b*emphasized text*

内容的提问来源于stack exchange,提问作者Darkoder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 13:41:23