window.parent.postMessage and window.location.hostname for t

window.parent.postMessage and window.location.hostname for the targetDomain

本文关键字:window for hostname and parent postMessage location      更新时间:2023-09-26

在子iframe中,我使用window.parent.postMessage('some msg', targetDomain)向父消息。最初,targetDomain是一个硬编码的"字符串"(例如,http://my.childdomain.com)。从那以后,我就被要求将targetDomain改为动态的,原因我们将在这里讨论。然后我尝试targetDomain作为http://'+window.location.hostname,但有些事情是不对的。

我把alert(window.location.hostname)放在前面,得到了正确的值,但window.parent.postMessage()似乎不像window.location.hostname

suggesions吗?

哎呀!问题解决了。主要问题是我没有解析window.location.hostname来获得TLD。一旦我睡了一会儿,恢复了理智,我就意识到我的方式是错误的。也就是说,我需要告诉window。parent。postmessage消息的去向,而不是它的来源。是啊,我知道…