你的位置:首页 > 软件开发 > Java > [转]JavaScript跨域总结与解决办法

[转]JavaScript跨域总结与解决办法

发布时间:2015-04-25 11:00:38
转载自http://www.cnblogs.com/rainman/archive/2011/02/20/1959325.html仅用作个人读书笔记。什么是跨域1、document.domain+iframe的设置2、动态创建script3、利用iframe和location.h ...

转载自http://www.cnblogs.com/rainman/archive/2011/02/20/1959325.html仅用作个人读书笔记。

  • 什么是跨域
  • 1、document.domain+iframe的设置
  • 2、动态创建script
  • 3、利用iframe和location.hash
  • 4、window.name实现的跨域数据传输
  • 5、使用HTML5 postMessage
  • 6、利用flash

什么是跨域

JavaScript出于安全方面的考虑,不允许跨域调用其他页面的对象。但在安全限制的同时也给注入iframe或是ajax应用上带来了不少麻烦。这里把涉及到跨域的一些问题简单地整理一下:

首先什么是跨域,简单地理解就是因为JavaScript同源策略的限制,a.com 域名下的js无法操作b.com或是c.a.com域名下的对象。更详细的说明可以看下表:

 
URL说明是否允许通信
http://www.a.com/a.js

script.a.com上的b.html

document.domain = 'a.com';

3、利用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值。代码如下:

先是a.com下的文件cs1.html文件:

function startRequest(){  var ifr = document.createElement('iframe');  ifr.style.display = 'none';  ifr.src = 'http://www.cnblogs.com/lab/cscript/cs2.html#paramdo';  document.body.appendChild(ifr);}function checkHash() {  try {    var data = location.hash ? location.hash.substring(1) : '';    if (console.log) {      console.log('Now the data is '+data);    }  } catch(e) {};}setInterval(checkHash, 2000);

cnblogs.com域名下的cs2.html:

//模拟一个简单的参数处理操作switch(location.hash){  case '#paramdo':    callBack();    break;  case '#paramset':    //do something……    break;}function callBack(){  try {    parent.location.hash = 'somedata';  } catch (e) {    // ie、chrome的安全机制无法修改parent.location.hash,    // 所以要利用一个中间的cnblogs域下的代理iframe    var ifrproxy = document.createElement('iframe');    ifrproxy.style.display = 'none';    ifrproxy.src = 'http://a.com/test/cscript/cs3.html#somedata';  // 注意该文件在"a.com"域下    document.body.appendChild(ifrproxy);  }}

a.com下的域名cs3.html

//因为parent.parent和自身属于同一个域,所以可以改变其location.hash的值parent.parent.location.hash = self.location.hash.substring(1);

b.com/index.html中的代码:

<script type="text/javascript">  window.addEventListener('message', function(event){    // 通过origin属性判断消息来源地址    if (event.origin == 'http://a.com') {      alert(event.data);  // 弹出"I was there!"      alert(event.source); // 对a.com、index.html中window对象的引用                 // 但由于同源策略,这里event.source不可以访问window对象    }  }, false);</script>

参考文章:《精通HTML5编程》第五章——跨文档消息机制、https://developer.mozilla.org/en/dom/window.postmessage

6、利用flash

这是从YUI3的IO组件中看到的办法,具体可见http://developer.yahoo.com/yui/3/io/。

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:[转]JavaScript跨域总结与解决办法

关键词:JavaScript

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。