JavaScript 跨域方式实现方式有很多,之前,一篇文章中提到了 JSONP 形式实现跨域。本文将介绍 HTML5 新增的 api 实现跨域:window.postMessage 。

 1 otherWindow.postMessage(message, targetOrigin);
2
3 otherWindow
4 其他窗口的一个引用,比如iframe的contentWindow属性、执行window.open返回的窗口对象、或者是命名过或数值索引的window.frames。
5
6 message
7 将要发送到其他 window的数据。将会被结构化克隆算法序列化。这意味着你可不受什么限制的安全传送数据对象给目标窗口而无需自己序列化。
8
9 targetOrigin
10 该参数可设置为 *,将无域名限制。也可指定特定的域名,比如:http://www.jj.com
11
12 https://developer.mozilla.org/zh-CN/docs/Web/API/Window/postMessage

本文还是以 iframe 为例,一个 html 页面中套有一个 iframe 文档,不过,iframe 文档 src 属性与 html 页面不同域,因此,两者之间便形成跨域。

index.html _http://localhost:3127/index.htm

<!DOCTYPE html >
<html >
<head>
<title></title>
</head>
<body >
<input type="text" id="val"/>
<input type="button" id="btn" value="测试" /><br/> <iframe src="http://localhost:10528/index.html" > </iframe>
<script type="text/javascript">
window.onload = function () {
// 首先,给按钮绑定 click 事件
if (window.addEventListener)
document.getElementById('btn').addEventListener('click', sendMsg, false);
else
document.getElementById('btn').attachEvent("onclick", sendMsg); };

// 获取 iframe 标签,利用 postMessage 跨域通信,值为 input text 标签值
function sendMsg() { document.getElementsByTagName('iframe')[0].contentWindow.postMessage(document.getElementById('val').value,"*"); } </script>
</body>
</html>

 index.html (iframe) _http://localhost:10528/index.html

<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title> </head>
<body >
<b>我是另一个跨中 iframe 文档</b>
<script type="text/javascript"> // 给 iframe 文档绑定 message 事件
window.onload = function () {
if (window.addEventListener)
window.addEventListener('message', getMessage, false);
else
window.attachEvent("onmessage", getMessage); };

// iframe 触发 message 事件时,调用 getMessage 函数
function getMessage(event) {
console.dir(event);
alert(event.data); } </script>
</body>
</html>

参考文档:https://developer.mozilla.org/zh-CN/docs/Web/API/Window/postMessage

JavaScript 跨域:window.postMessage 实现跨域通信的更多相关文章

  1. 解决Iframe跨域高度自适应,利用window.postMessage()实现跨域消息传递页面高度(JavaScript)

    在iframe跨域引用高度自适应这块写的js方式都试了不管用,最终使用的是window.postMessage() 跨域获取高度 传递信息 1.首先,在主页面上使用iframe引入子页面:也就是A.h ...

  2. 使用window.postMessage实现跨域通信

    JavaScript由于同源策略的限制,跨域通信一直是棘手的问题.当然解决方案也有很多: document.domain+iframe的设置,应用于主域相同而子域不同: 利用iframe和locati ...

  3. 利用HTML5的window.postMessage实现跨域通信

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp77   HTML5的window.postMessage简述 postM ...

  4. window.postMessage()实现跨域消息传递

    window.postMessage() 方法可以安全地实现跨源通信.通常,对于两个不同页面的脚本,只有当执行它们的页面位于具有相同的协议(通常为https), 端口号(443为https的默认值), ...

  5. window.postMessage实现网页间通信

    window.postMessage() 方法可以安全地实现跨域通信.通常,对于两个不同页面的脚本,只有当执行它们的页面位于具有相同的协议(通常为https),端口号(443为https的默认值),以 ...

  6. js实现跨域(jsonp, iframe+window.name, iframe+window.domain, iframe+window.postMessage)

    一.浏览器同源策略 首先我们需要了解一下浏览器的同源策略,关于同源策略可以仔细看看知乎上的一个解释.传送门 总之:同协议,domain(或ip),同端口视为同一个域,一个域内的脚本仅仅具有本域内的权限 ...

  7. (二)文档请求不同源之window.postMessage跨域

    一.基本原理 HTML5为了解决跨域,引入了跨文档通信API(Cross-document messaging).这个API为window对象新增了一个window.postMessage方法,允许跨 ...

  8. 跨域通信--Window.postMessage()

    一.跨源通信概述 源:协议.端口号(https默认值433).主机域名(document.domain) 作用:向目标窗口派发MessageEvent消息(四个属性) 兼容参考 MessageEven ...

  9. HTML5之worker开启JS多线程模式及window.postMessage跨域

    worker概述 worker基本使用 window下的postMessage worker多线程的应用 一.worker概述 web worker实际上是开启js异步执行的一种方式.在html5之前 ...

随机推荐

  1. cscope 的使用

    一. cscope安装 1.软件下载:http://sourceforge.net/project/showfiles.php?group_id=4664 2.软件安装: ./configure -- ...

  2. RequireJS入门指导 (转)

    最近在百度实习做的一个项目用到了 Require JS 这个库,之前从来没有了解过,经过一番大概的搜索后找到一篇非常不错的文章,看完后能够让你对 Require JS 的运行机制.使用方法以及为什么使 ...

  3. 嵌入式 hi3518平台检测网线是否插上

    /********************************** (C) COPYRIGHT ******************************* * File Name        ...

  4. C#读写文件总结

    1.使用FileStream读写文件   文件头:   using System; using System.Collections.Generic; using System.Text; using ...

  5. LR之错误处理

    1.脚本的健壮性 2.VuGen的处理机制 3.lr_continue_on_error函数 4.示例代码

  6. Solaris系统管理(一)

    最近需要将一个项目从Linux平台迁移到Solaris,对Solaris进行了一点研究,总结如下. 一句话介绍: Solaris 是Sun Microsystems研发的计算机操作系统.它被认为是UN ...

  7. Leetcode: Length of Last Word in python

    Length of Last Word Total Accepted: 47690 Total Submissions: 168587     Given a string s consists of ...

  8. static public和 public static 区别

    static:加static 的是静态成员,不能实例化在你运行的时候他自己在内存中开辟了块空间,不用在new, 有点像全局变量,如果不用你必须去 实例化(new)才能用 static是静态的意思,pu ...

  9. Home-brew 安装&卸载 Git

    安装 brew install git 卸载git: rm -rf /usr/local/git rm /etc/paths.d/git rm /etc/manpaths.d/git sudo rm ...

  10. Linux下oracle导入(exp)导出(imp)出现"Failed to open ...for reader/write"错误