js中的跨域方法总结

什么是跨域?

浏览器的安全策略,只要协议,域名,端口有任何一个不同,就被当做不同的域。

下面对http://www.qichedaquan.com的同源检测

http://www.qichedaquan.com/a.html  // 同源

http://www.qichedaquan.com/b.html  // 同源

http://www.qichedaquan.com:8080   //不同源

http://news.qichedaquan.com/          //不同源

http://pic.qichedaquan.com/             //不同源

1,jsonp方法

原理:利用script标签,在页面上引入不同域上的js脚本文件

2,document.domain

原理:document.domain设置成相同的域名,来访问iframe中的属性和对象

3,window.name

原理: 在一个窗口的生命周期内,窗口载入的所有页面都是共享一个window.name,每个页面对window.name都有读写的权限

4,window.postMessage

原理: H5的新属性,不支持IE6,7

原文地址:https://www.cnblogs.com/zhangbs/p/9675740.html