且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

跨站点脚本 Iframe 权限被拒绝问题

更新时间:2022-11-25 16:40:28

除了 Christophe 的回答,我想指出(遗憾的是)postMessage 不适用于所有浏览器.

In addition to the answer of Christophe, I wanted to point out (sadly) postMessage doesn't work on all browsers.

幸运的是,Josh Fraser 已经提供了window.postMessage() 的向后兼容版本.它检查浏览器是否支持 postMessage 方法.如果是,它会使用它.如果没有,它会使用 URL(来自 iframe 和父级)来传递数据.

Luckily, Josh Fraser already provided a backwards compatible version of window.postMessage(). It checks if the browser supports the postMessage-method. If it does, it uses that. If not, it uses the URL (both from the iframe and the parent) to pass along data.

现在您可以使用以下方法让两个窗口相互交谈":

Now you can use the following methods to let both windows "talk" to eachother:

XD.postMessage(msg, src, frames[0]);
XD.receiveMessage(function(message){
    window.alert(message.data + " received on "+window.location.host);
}, 'URL');

请确保您正确阅读文档,因为必须正确设置配置.

Just make sure you read the documentation properly, since the configuration has to be set just right.