由于同源
策略的局限性,Javascript一直是跨域通信中的难题,当然也有很多
解决方案。
1
设置文档。域+ iframe应用于同一主域和子域是不同的。
2。使用iframe和location.hash,数据直接暴露在URL中,有限的数据容量和
类型 3。Flash LocalConnection,对象可以在SWF
文件或多个SWF文件之间的沟通,只要
在同一个客户机上,可以跨
应用程序,并且可以跨域。
window.name
保存数据和跨域iframe静态
代理动态
传输方案,充分利用window.name不会改变,因为页面的URL变化特征。
网上有很多代码的例子,你可以自己
搜索。
One of the most cool API in HTML5: Cross Document is a cross document messaging Messaging.Advanced browsers Internet Explorer 8+, chrome, Firefox, Opera, and Safari will support this feature.The implementation of this function is also very simple, mainly including the message events that accept information and the postMessage method for sending messages.
发送
消息发送消息的
方法 将消息发送到外部窗口:
复制代码代码如下:otherwindow.postmessage(消息,targetorigin);
otherwindow:指
目标窗口,并发送消息给窗口,window.frames的成员
属性或由window.open方法
创建窗口
参数描述:
1.message:是要发送的消息,字符串类型,对象(IE8,9不
支持)
2.targetorigin:是新闻有限的接收范围,请使用*是不受限制的
消息事件接收信息
复制代码代码如下所示:
无功
控制=
函数(事件){
VaR数据= event.data;
var = event.origin起源;
/ /做
};
如果(typeof window.addeventlistener!= 'undefined){
Window.addEventListener(消息、消息、假);
} else if(typeof window.attachevent!= 'undefined){
IE
浏览器
window.attachevent('onmessage消息);
}
回调函数的第一个参数接收事件对象,它有三个公共属性:
1.data:消息
2.origin:报文的源
地址 3.source:源domwindow对象
当然,PostMessage也有一些不足,太。
数据类型值通过1.ie8 IE9支持字符串类型,但是你可以通过将JSON对象和字符串的解决这个问题。
2.ie6,IE7兼容方案需要写,我想window.name更可靠。