HTML5 postMessage 跨域窗口信息传递
来源:互联网 发布:oracle12c新建数据库 编辑:程序博客网 时间:2024/05/19 17:08
认识这个API,还要从需求说起。
公司由个前端页面是使用ifreame嵌入在另一家公司页面中的,这家公司由一个切换主题的按钮,而点击切换主题,我们的页面也需要进行主题切换。而他们的服务和我们的服务是分开的,此时窗口间的信息传递就不能使用iframe框架集随意传递了,于是找到了postMessage。
先看一下MDN的说明:
window.postMessage() 方法可以安全地实现跨源通信。通常,对于两个不同页面的脚本,只有当执行它们的页面位于具有相同的协议(通常为https),端口号(443为https的默认值),以及主机 (两个页面的模数 Document.domain设置为相同的值) 时,这两个脚本才能相互通信。window.postMessage() 方法提供了一种受控机制来规避此限制,只要正确的使用,这种方法就很安全。
说的已经很明白了,完全能解决我的需求,下面直接上代码:
// 发送contentWindow.postMessage('this is a message','http://www.example.com')
// 接收window.addEventListener('message', function(e){ if(e.source !== window.parent) return let value = e.data //...}, false)
如果想看更详细的信息,可查看这个博客。
如果要看标准,MDN走你
阅读全文
0 0
- HTML5 postMessage 跨域窗口信息传递
- html5 postMessage解决跨域、跨窗口消息传递
- html5 postMessage解决跨域、跨窗口消息传递
- html5 postMessage解决跨域、跨窗口消息传递
- HTML5 postMessage解决跨域、跨iframe窗口消息传递
- html5 postMessage解决跨域、跨窗口消息传递
- html5 postMessage解决跨域、跨窗口消息传递
- html5 postMessage解决跨域、跨窗口消息传递
- html5 postMessage解决跨域、跨窗口消息传递
- html5 postMessage解决跨域、跨窗口消息传递
- html5 postMessage解决跨域、跨窗口消息传递
- html5 postMessage解决跨域和跨窗口消息传递
- html5 postMessage解决跨域、跨窗口消息传递
- html5 postMessage解决跨域、跨窗口消息传递
- html5 postMessage解决跨窗口/跨域消息传递
- html5 postMessage解决跨域、跨窗口消息传递
- html5 postMessage解决跨域、跨窗口消息传递
- postMessage 解决窗口跨域消息传递
- 简单随机验证码的生成
- 关于EditText、AutoCompleteTextView类似这类控件的简单输入限制
- Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushM
- PHP PDO+MySQL实现登录注册页面
- hibernate约束文件的导入步骤
- HTML5 postMessage 跨域窗口信息传递
- 2017年09月23日普级组 数列
- LintCode-分治-Expression Expand
- COMMTIMEOUTS结构
- SpringMVC学习笔记(3)-----表单处理
- android-什么是GlibC
- cenos 6.8 安装samba4.7
- C++单例模式(自动释放实例)
- 玻尔兹曼机通俗理解