如何在 safari 中发送跨域邮件? [英] how to send cross domain post message in safari?
本文介绍了如何在 safari 中发送跨域邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
根据 canIuse Safari 9 支持跨域 postMessage 但是当我尝试将消息发送到不同的域我有关于来源的异常.
According to canIuse Safari 9 support Cross domain postMessage but when I try to send message to different domain I've got exception about origin.
我有这样的代码:
var input = document.getElementsByTagName('input')[0];
// this line throw exception
var win = document.getElementsByTagName('iframe')[0].contentWindow;
a.onclick = function() {
var obj = {message: input.value};
sysend.broadcast('foo', obj);
// proxy to send message to other domains using hidden iframe
win.postMessage(JSON.stringify({key: 'foo', data: obj}), "*");
};
显示此错误的页面是http://jcubic.pl/sysend.php
推荐答案
Safari 第一次访问 contentWindow 时似乎抛出了异常,第二次之后就正常了:
It seems that Safari throw exception when accessing contentWindow first time, it work after second time:
var iframe = document.getElementsByTagName('iframe')[0];
var win;
try {
win = iframe.contentWindow;
} catch(e) {
win = iframe.contentWindow;
}
这篇关于如何在 safari 中发送跨域邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文