六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 117|回复: 0

HTML5 postMessage

[复制链接]

升级  84%

50

主题

50

主题

50

主题

秀才

Rank: 2

积分
176
 楼主| 发表于 2013-1-29 13:46:03 | 显示全部楼层 |阅读模式
      在HTML5中新增了postMessage方法,postMessage可以实现跨文档消息传输(Cross Document Messaging),Internet Explorer 8, Firefox 3, Opera 9, Chrome 3和 Safari 4都支持postMessage。
  可以通过绑定window的message事件来监听发送跨文档消息传输内容。示例代码如下:
 eg.) parent page
<!DOCTYPE HTML><html><head><title>Communication</title><script>    var messageChange = function(e) {        var data = e.data;        var origin = e.origin;        if (origin !== "http://www.example.org") return;      document.getElementById('display').innerHTML = data;            };            if (typeof window.addEventListener != 'undefined') {        window.addEventListener('message', messageChange, false);      } else if (typeof window.attachEvent != 'undefined') {        window.attachEvent('onmessage', messageChange);      }  </script></head><body><div id="display">Say something!</div><iframe scrolling="no" frameborder="0" width="0" height="0" src="http://www.example.org/html5/postMessage/sub.html"></iframe>  </body></html>  eg.) sob page
<!DOCTYPE HTML><html><head><title>Communication</title><script>setInterval(function(){    window.parent.postMessage("hello world!", "http://www.example.org");},1000);</script></head><body></body></html> 




   DEMO见附件, 更多HTML5相关demo地址:http://html5demos.com/
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表