【问题标题】:How can I send information from an embeded iframe to my website( where that iframe is embeded on)如何将信息从嵌入的 iframe 发送到我的网站(嵌入 iframe 的位置)
【发布时间】:2021-01-16 10:45:40
【问题描述】:

我正在编写一个不想嵌入 iframe 的网站,iframe 是在不同的域/来源上编码的,我不想每次点击时都向我的网站(网站的 javascript)发送字符串一个按钮,我不能使用 sessionStorage 或 localstorage,因为 iframe 与网站不在同一个来源!

更新

我尝试过使用 postmessage,这种方法对我来说是新的,我已经能够接收从控制台发送的消息,但由于某种原因我无法从 iframe 发送消息,它不起作用这是需要发送消息的按钮的代码<img id="play"src="https://image source" alt="play" onclick="window.postMessage('{{YTID}}', '*');" height="100" width="100"> 按钮显示,但不会发送。

我认为这与 window 有关,但我不知道接收窗口的正确 DOM 需要是什么。

【问题讨论】:

  • 哇,谢谢,我已经在我的站点中实现了这个,但由于某种原因,它不会发送,我可以读取从控制台发送的消息并使用它们,但它不会发送从 iframe ,我使用 onclick="window.postMessage(ID, '*');" , ID 是我不想发送的变量。
  • 更新(编辑)您的问题,提供您当前(和相关)的代码。

标签: javascript html iframe storage embed


【解决方案1】:

我用过发帖方式more about that here

在发送端我使用window.parent.postMessage('Message', '*'); window.parent 指定将消息发送到 iframe 的父级,这是我的网站,在接收端(在我的网站上)我使用 window.addEventListener("message", function(message){ more code });

这对我有用,谢谢

【讨论】:

    猜你喜欢
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    • 2023-02-04
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    相关资源
    最近更新 更多