简体   繁体   English

在重新加载的 iframe 中使用 postMessage 不起作用

[英]Using postMessage in reloaded iframe not working

I have iframe with simple form (provided by third-party vendor).我有简单形式的 iframe(由第三方供应商提供)。 The content of iframe reloads after form is submitted with confirmation page. iframe 的内容在表单提交确认页面后重新加载。 On that page I asked provider to add this script:在该页面上,我要求提供者添加此脚本:

<script type="text/javascript">
    window.setTimeout(function(){
        window.parent.postMessage('mailing_list_confirmation','http://originURL.com/');
    },100) </script>

I the top window I have:我是顶级 window 我有:

<script>
    jQuery(function($) {
    $(window).on('mailing_list_confirmation',function(){
          console.log("iframe_message");
       });
    });
</script>

Nothing complicated.没什么复杂的。 I tried also with plain JS.我也尝试过使用普通的 JS。 I tried this on my localhost (2 different domains) and it was working well.我在我的本地主机(2 个不同的域)上尝试了这个,它运行良好。 The difference was that the iframe was originally loaded.不同之处在于最初加载的是 iframe。 In this case the iframe content is new page.在这种情况下,iframe 内容是新页面。 Is there any other reason why this might not work?还有其他原因导致这可能不起作用吗?

That should work, check the domain your posting to is what you think it is and not www.originaldomain.com.这应该可行,检查您发布的域是您认为的域,而不是 www.originaldomain.com。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM