- 浏览: 303911 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
popotang:
关注raphael,从中学习一下
基于jquery, raphael的WEB流程设计器 -
Getwaysun:
拜读了,写得非常好
iframe跨域session丢失问题 -
chen4w:
good!
Geoserver & Openlayers 学习笔记 -
holysky:
Caused by: org.springframework. ...
Spring对属性文件加密解密应用 -
holysky:
希望能解决一下!
Spring对属性文件加密解密应用
什么是跨域
JavaScript出于安全方面的考虑,不允许跨域调用其他页面的对象。但在安全限制的同时也给注入iframe或是ajax应用上带来了不少麻烦。这里把涉及到跨域的一些问题简单地整理一下:
首先什么是跨域,简单地理解就是因为JavaScript同源策略的限制,a.com 域名下的js无法操作b.com或是c.a.com域名下的对象。更详细的说明可以看下表:
URL的首部指window.location.protocol +window.location.host,也可以理解为Domains, protocols and ports must match。
接下来简单地总结一下在前台一般处理跨域的办法,后台proxy这种方案牵涉到后台配置,这里就不阐述了,有兴趣的可以看看yahoo的这篇文章:《JavaScript: Use a Web Proxy for Cross-Domain XMLHttpRequest Calls》
1、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文件之间就可以交互了。当然这种办法只能解决主域相同而二级域名不同的情况,如果你异想天开的把script.a.com的domian设为alibaba.com那显然是会报错地!代码如下:
www.a.com上的a.html
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->document.domain = 'a.com'; var ifr = document.createElement('iframe'); ifr.src = 'http://script.a.com/b.html'; ifr.style.display = 'none'; document.body.appendChild(ifr); ifr.onload = function(){ var doc = ifr.contentDocument || ifr.contentWindow.document; // 在这里操纵b.html alert(doc.getElementsByTagName("h1")[0].childNodes[0].nodeValue); }; 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。
2、动态创建script
虽然浏览器默认禁止了跨域访问,但并不禁止在页面中引用其他域的JS文件,并可以自由执行引入的JS文件中的function(包括操作cookie、Dom等等)。根据这一点,可以方便地通过创建script节点的方法来实现完全跨域的通信。具体的做法可以参考YUI的Get Utility
这里判断script节点加载完毕还是蛮有意思的:ie只能通过script的readystatechange属性,其它浏览器是script的load事件。以下是部分判断script加载完毕的方法。
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->js.onload = js.onreadystatechange = function() { if (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete') { // callback在此处执行 js.onload = js.onreadystatechange = null; } };
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文件:
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->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
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->//因为parent.parent和自身属于同一个域,所以可以改变其location.hash的值 parent.parent.location.hash = self.location.hash.substring(1);
当然这样做也存在很多缺点,诸如数据直接暴露在了url中,数据容量和类型都有限等……
4、window.name实现的跨域数据传输
文章较长列在此处不便于阅读,详细请看 window.name实现的跨域数据传输。
5、使用HTML5 postMessage
HTML5中最酷的新功能之一就是 跨文档消息传输Cross Document Messaging。下一代浏览器都将支持这个功能:Chrome 2.0+、Internet Explorer 8.0+, Firefox 3.0+, Opera 9.6+, 和 Safari 4.0+ 。 Facebook已经使用了这个功能,用postMessage支持基于web的实时消息传递。
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->otherWindow.postMessage(message, targetOrigin);
message: 所要发送的数据,string类型。
targetOrigin: 用于限制otherWindow,*表示不作限制
a.com/index.html中的代码:
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> iframe id="ifr" src="b.com/index.html" /iframe script type="text/javascript" window.onload = function() { var ifr = document.getElementById('ifr'); var targetOrigin = 'http://b.com'; // 若写成'http://b.com/c/proxy.html'效果一样 // 若写成'http://c.com'就不会执行postMessage了 ifr.contentWindow.postMessage('I was there!', targetOrigin); }; /script
b.com/index.html中的代码:
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->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
6、利用flash
这是从YUI3的IO组件中看到的办法,具体可见http://developer.yahoo.com/yui/3/io/。
可以看在Adobe Developer Connection看到更多的跨域代理文件规范:ross-Domain Policy File Specifications、HTTP Headers Blacklist。
发表评论
-
产品原型设计工具-Balsamiq Mockups
2014-04-04 11:16 820Balsamiq Mockups是一种软件工程中快速原 ... -
JQuery美化下拉框插件
2014-01-19 11:01 809<!DOCTYPE html> <htm ... -
jBPM Web Designer(jBPM Web 设计器、jBPM 流程设计器、工作流程设计器)
2013-08-28 07:21 1249大多研究JBPM的,对其引擎的扩展开发都不曾苦恼,但提及其可 ... -
一些基于ExtJS的Desktop(桌面)特效应用的网站
2011-11-11 14:08 1266http://qwikioffice.com/desk ... -
JavaScript复制文字或图片到剪切板
2011-09-28 17:00 38901、复制图片到剪切板 function CopyI ... -
YUI Compressor 使用
2011-08-18 15:32 2061最近项目需求,要压缩j ... -
Nconvert 给图片加水印图片
2011-08-03 17:07 1012nconvert -wmpos -15 -35 -wmflag ... -
jquery.form.js插件中ajaxSubmit提交在jquery1.4版本中的应用
2011-07-28 10:37 2009下以处理的是对含有 <input type" ... -
判断img是否加载完成
2011-05-26 07:57 1118经常用到判断某个img是否加载完成,然后执行一些事件 fu ... -
一张图片变化多种花样
2011-03-29 20:32 1202web2.0的世界中,javascript的作用日趋重大,集 ... -
javascript图片浏览器的核心——图片预加载
2011-03-29 20:30 955网站开发时经常需要在某个页面需要实现对大量图片的浏览,如果考 ... -
highchart
2010-10-21 09:00 1118highchart -
Javascript跨域访问解决方案
2010-09-15 09:17 897由于安全方面的考虑,Javascript被限制了跨域访问的能力 ... -
网页天气预报插件
2010-09-06 14:54 1540http://www.thinkpage.cn/ ... -
JQUERY获取form表单text,areatext,radio,checkbox,select值
2010-08-02 15:20 1337jquery取得text,areatext,radio ... -
jquery和prototype冲突解决
2010-08-02 10:34 1066问题背景: 想用 jQuery 做 AJAX 处理, 用基于 ... -
JS Cookie详解
2009-07-27 15:05 1224一、cookie概述 cookie是 ... -
highslide JS图片浏览特效
2009-04-19 18:58 3612highslide 图片浏览特效说 ...
相关推荐
本篇文章主要介绍了JavaScript跨域,详细的总结了什么是跨域,并介绍了几种解决办法,感兴趣的小伙伴们可以参考一下。
主要介绍了javascript 跨域问题以及解决办法的相关资料,需要的朋友可以参考下
Javascript跨域访问解决方案 个人在网上搜集的资料,用于传输信息,不提倡下载
ajax跨域问题的解决办法
NULL 博文链接:https://javasam.iteye.com/blog/1849358
JavaScript 父子页面跨域通信的方法
实现javascript跨域,可以在不同域名双向通信,内含demo,支持IE6+、火狐,谷歌等浏览器 配套文章:http://blog.csdn.net/mycwq/article/details/16344171
在浏览器查看一个pdf文件时常常不知道怎么显示,这个资源可以很好的帮到你,而且还附有跨域的解决办法。
NULL 博文链接:https://sun123start.iteye.com/blog/2150778
主要介绍了javascript跨域原因以及解决方案分享,十分的细致全面,有需要的小伙伴可以参考下。
JS跨域访问解决方案总结.。总结了记住JS跨域访问解决方案。
arcgis api for javascript跨域处理方案
js跨域访问解决方案总结,不同域名下cookie相互操作
本资源主要介绍了AJAX跨域的解决办法,以及解决方案间的比较
NULL 博文链接:https://swordshadow.iteye.com/blog/2082999
解决tomcat在IP 和端口不同时引起的跨域问题,解决方案,通过编写crossDomain.xml文件