gpt4 book ai didi

javascript - 如何访问postMessage中发送的数据?

转载 作者:行者123 更新时间:2023-12-03 07:09:55 25 4
gpt4 key购买 nike

我需要在 iFrame 中运行 JavaScript,我知道我必须使用发布消息来执行此操作,但我不明白如何使用它。

站点 1 是 http://www.example.com/chat.html站点 2(站点 1 上的 iFrame)是 http://www.example.com:7778

网站

$( "#link" ).click(function() {
document.getElementById('iframe_id_here').contentWindow.postMessage({command: 'joinChannel', args: {channel: $(this).attr('data-room')}}, '*');
});

iFrame

function joinChannel(event)
{
if (event.origin !== "http://example.com/chat.html")
return;

network.join(***How do I access $(this).attr('data-room')?***, '');
}

window.addEventListener("message", joinChannel, false);

最佳答案

window.postMessage ,传递的消息存储在 event.data 对象中。

在您的情况下,应该可以通过 event.data.args.channel 访问该 channel 。

关于javascript - 如何访问postMessage中发送的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36650185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com