什么是跨域?
JavaScript出于安全方面的考虑,不允许跨域调用其他页面的对象。但在安全限制的同时也给注入iframe或是ajax应用上带来了不少麻烦http://www.a.com/a.js
http://www.a.com/b.js 同一域名下 允许
http://www.a.com/lab/a.js
http://www.a.com/script/b.js 同一域名下不同文件夹 允许
http://www.a.com:8000/a.js
http://www.a.com/b.js 同一域名,不同端口 不允许
http://www.a.com/a.js
https://www.a.com/b.js 同一域名,不同协议 不允许
http://www.a.com/a.js
http://70.32.92.74/b.js 域名和域名对应ip 不允许
http://www.a.com/a.js
http://script.a.com/b.js 主域相同,子域不同 不允许
http://www.a.com/a.js
http://a.com/b.js 同一域名,不同二级域名(同上) 不允许(cookie这种情况下也不允许访问)
http://www.cnblogs.com/a.js
http://www.a.com/b.js 不同域名 不允许跨域方法汇总
jsonp
jsonp跨域还是比较常用的,具体可以参照我之前的博客jsonp跨域document.domain + iframe
对于主域相同而子域不同的例子,可以通过设置document.domain的办法来解决。具体的做法是可以在http://www.a.com/a.html和http://script.a.com/b.html两个文件中分别加上document.domain = ‘a.com’;然后通过a.html文件中创建一个iframe,去控制iframe的contentDocument,这样两个js文件之间就可以“交互”了。
www.a.com 的a.html
document.domain = 'a.com'; var iframe = document.createElement('iframe'); iframe.src = 'http://script.a.com/b.html'; iframe.style.display = 'none'; document.body.appendChild(iframe); iframe.onload=function(){ var doc = iframe.contentDocument||iframe.contentWindow.document; //在这里操作b.html console.log(doc.getElementByTagName('h1')[0]); }
script.a.com下的b.html
document.domain = 'a.com';
这种方式适用于{www.kuqin.com, kuqin.com, script.kuqin.com, css.kuqin.com}中的任何页面相互通信。
备注:某一页面的domain默认等于window.location.hostname。主域名是不带www的域名,例如a.com,主域名前面带前缀的通常都为二级域名或多级域名,例如www.a.com其实是二级域名。 domain只能设置为主域名,不可以在b.a.com中将domain设置为c.a.com。
问题:
1、安全性,当一个站点(b.a.com)被攻击后,另一个站点(c.a.com)会引起安全漏洞。
2、如果一个页面中引入多个iframe,要想能够操作所有iframe,必须都得设置相同domain。动态创建script
iframe + location.hash
这个办法比较绕,但是可以解决完全跨域情况下的脚步置换问题。原理是利用location.hash来进行传值。在url: http://a.com#helloword中的‘#helloworld’就是location.hash,改变hash并不会导致页面刷新,所以可以利用hash值来进行数据传递,当然数据容量是有限的。假设域名a.com下的文件cs1.html要和cnblogs.com域名下的cs2.html传递信息,cs1.html首先创建自动创建一个隐藏的iframe,iframe的src指向cnblogs.com域名下的cs2.html页面,这时的hash值可以做参数传递用。cs2.html响应请求后再将通过修改cs1.html的hash值来传递数据(由于两个页面不在同一个域下IE、Chrome不允许修改parent.location.hash的值,所以要借助于a.com域名下的一个代理iframe;Firefox可以修改)。同时在cs1.html上加一个定时器,隔一段时间来判断location.hash的值有没有变化,一点有变化则获取获取hash值。//cs1.html function startRequest(){ var iframe = document.createElement('iframe'); iframe.src="http://www.cnblogs.com/lab/cscript/cs2.html#test"; //cs2.html根据test来改变其location.hash iframe.style.display='none'; document.body.appendChild(iframe); } //当location.hash被改变之后,进行相应的处理 function checkHash(){ try{ var data = location.hash?location.hash.subString(1):""; if(console.log){ console.log(data); } }catch(e){ } } //定时检测location.hash的变化 setInterval(checkHash,2000); //cs2.html switch(location.hash){ case:'#test': callback(); break; case:'#exam': //do something break; } function callback(){ try{ parent.location.hash = "some data"; }catch(e){ // ie、chrome的安全机制无法修改parent.location.hash, // 所以要利用一个中间的cnblogs域下的代理iframe var proxy = document.createElement('iframe'); iframe.style.display = 'none'; iframe.src="http://a.com/test/cscript/cs3.html#somedata" // 注意该文件在"a.com"域下 somedata用来设定的值 document.body.appendChild(proxy); } } //cs3.html //因为parent.parent和自身属于同一个域,所以可以改变其location.hash的值 parent.parent.location.hash = self.location.hash.sunString(1);
当然这样做也存在很多缺点,诸如数据直接暴露在了url中,数据容量和类型都有限等……
window.name
a.com/app.html:应用页面。
a.com/proxy.html:代理文件,一般是一个没有任何内容的html文件,需要和应用页面在同一域下。
b.com/data.html:应用页面需要获取数据的页面,可称为数据页面。
实现起来基本步骤如下:在应用页面(a.com/app.html)中创建一个iframe,把其src指向数据页面(b.com/data.html)。
数据页面会把数据附加到这个iframe的window.name上,// data.html window.name = "数据都在这"; // 这里是要传输的数据,大小一般为2M,IE和firefox下可以大至32M左右 // 数据格式可以自定义,如json、字符串 // 在应用页面(a.com/app.html)中监听iframe的onload事件,在此事件中设置这个iframe的src指向本地域的代理文件 // (代理文件和应用页面在同一域下,所以可以相互通信)。 // app.html var state = 0; var iframe = document.createElement('script'); iframe.style.display = 'none'; iframe.src = "http://b.com/data.html"; if(iframe.attachEvent){ iframe.attachEvent('onload',loadfn); }else{ iframe.onload = loadfn; } document.body.appendChild(iframe); loadfn = function(){ if(state === 0){ state=1; iframe.contentWindow.location = "http://a.com/proxy.html"; //设置代理页面,避免泄漏应用页面的网址 } else if(state === 1){ var data = iframe.contentWindow.name; //由于代理页面和应用页面在同一个域下,所以可以直接访问数据 console.log(data); } } //读取数据之后销毁iframe,释放内存 iframe.contentWindow.document.write(''); iframe.contentWindow.close(); document.body.removeChild(iframe);
Html5 postMessage
HTML5中最酷的新功能之一就是 跨文档消息传输Cross Document MessagingotherWindow.postMessage(message, targetOrigin);
otherWindow: 对接收信息页面的window的引用。可以是页面中iframe的contentWindow属性;window.open的返回值;通过name或下标从window.frames取到的值。
message: 所要发送的数据,string类型。
targetOrigin: 用于限制otherWindow,“*”表示不作限制//a.com/index.html <iframe src="b.com/index.html" id="ifr"> </iframe> <script> window.onload = function(){ var iframe = document.getElementById('ifr'); var targetOrigin = 'http://b.com'; // 若写成'http://b.com/c/proxy.html'效果一样 // 若写成'http://c.com'就不会执行postMessage了 iframe.contentWindow.postMessage('data to send',targetOrigin); } </script> // b.com/index.html <script type="text/javascript"> window.addEventListener('message',function(event){ // 通过origin属性判断消息来源地址 if(event.origin == 'http://a.com'){ console.log(event.data); console.log(event.source); } },false); </script>
跨域详解
最新推荐文章于 2024-07-06 09:00:00 发布