document.domain与js跨域的问题
以前如果要使iframe里面的脚本能访问parent的内容,但iframe和parent的二级域名相同,那一般都会在两者都写上document.domain="xxx.com" 以放宽访问权限。
今天发现,如果iframe和parent在同一个三级域名下,比如都是aa.bb.com,那设了document.domain反而会造成访问拒绝。
查了下MSDN,有如下解释:
Remarks
The property initially returns the host name of the server from which the page is served. The property can be assigned the domain suffix to allow sharing of pages across frames. For example, a page in one frame from home.microsoft.com and a page from www.microsoft.com initially would not be able to communicate with each other. However, by setting the domain property of both pages to the suffix "microsoft.com", you ensure that both pages are considered secure and access is available between the pages.
When you set the domain property, use the domain name determined by the server rather than by the client browser.
All the pages on different hosts must have the domain property explicitly set to the same value to communicate successfully with each other. For example, the value of the domain property of a page on the host microsoft.com would be "microsoft.com" by default. It might seem logical that if you set the domainproperty of a page on another host named msdn.microsoft.com to "microsoft.com," that the two pages could communicate with each other. However, this is not the case unless you have also explicitly set the domain property of the page on microsoft.com to "microsoft.com".
Furthermore, this property cannot be used to allow cross-frame communication among frames with different domain suffixes. For example, a page in one frame from www.microsoft.com and a page in another frame from www.msn.com would not be able to communicate with each other even if the domain property of both pages was set to the suffix "microsoft.com".
Security Alert Using this property incorrectly can compromise the security of your Web site. Set the domain property only if you must allow cross-domain scripting. Use a value determined on the server. Setting this property to a value determined on the client (like through the location object) could expose your site to attack from another site through Domain Name System (DNS) manipulation. For more information, see Security Considerations: Dynamic HTML.
For more information on domain security, see About Cross-Frame Scripting and Security.
document.domain通俗讲解,没验证过,不知道正确不?
比如在地址栏里输入:
javascript:alert(document.domain); //www.315ta.com
我们也可以给document.domain属性赋值,不过是有限制的,你只能赋成当前的域名或者基础域名。
比如:
javascript:alert(document.domain = "315ta.com"); //315ta.com
javascript:alert(document.domain = "www.315ta.com");//www.315ta.com
上面的赋值都是成功的,因为www.315ta.com是当前的域名,而315ta.com是基础域名。
但是下面的赋值就会出来"参数无效"的错误:
javascript:alert(document.domain = "cctv.net"); //参数无效
javascript:alert(document.domain = "ttt.315ta.com"); //参数无效
因为cctv.net与ttt.315tas.com不是当前的域名也不是当前域名的基础域名,所以会有错误出现。
这是为了防止有人恶意修改document.domain来实现跨域偷取数据。
利用document.domain实现跨域:
前提条件:这两个域名必须属于同一个基础域名!而且所用的协议,端口都要一致,否则无法利用document.domain进行跨域
Javascript出于对安全性的考虑,而禁止两个或者多个不同域的页面进行互相操作,~~~协议 主机名 和 端口号都相同才是相同的域,否则就是不同的域
相同域的页面在相互操作的时候不会有任何问题。
比如在:aaa.com的一个网页(a.html)里面利用iframe引入了一个bbb.com里的一个网页(b.html)。
这时在a.html里面可以看到b.html里的内容,但是却不能利用javascript来操作它。因为这两个页面属于不同的域,在操作之前,js会检测两个页面的域是否相等,如果相等,就允许其操作,如果不相等,就会拒绝操作。
这里不可能把a.html与b.html利用JS改成同一个域的。因为它们的基础域名不相等。(强制用JS将它们改成相等的域的话会报跟上面一样的"参数无效错误。")
所以如果在a.html里引入aaa.com里的另一个网页,是不会有这个问题的,因为域相等。 ~~~相同域的2个页面可以相互通信
有另一种情况,两个子域名:
aaa.xxx.com
bbb.xxx.com
aaa里的一个网页(a.html)引入了bbb 里的一个网页(b.html),
这时a.html里同样是不能操作b.html里面的内容的。
因为document.domain不一样,一个是aaa.xxx.com,另一个是bbb.xxx.com。 ~~~基础域名是相同的 所以可以设置 document.domain = 基础域名,使之可通信
这时我们就可以通过Javascript,将两个页面的domain改成一样的,
需要在a.html里与b.html里都加入:
document.domain = "xxx.com";
这样这两个页面就可以互相操作了。也就是实现了同一基础域名之间的"跨域"。
document.domain与js跨域的问题的更多相关文章
- 设置document.domain实现js跨域注意点
转自:http://www.cnblogs.com/fsjohnhuang/archive/2011/12/07/2279554.html document.domain 用来得到当前网页的域名.比如 ...
- JS跨域(ajax跨域、iframe跨域)解决方法及原理详解(jsonp)
这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被 ...
- 前端Js跨域方法汇总—剪不断,理还乱,是跨域
1.通过jsonp跨域2.通过修改document.domain来跨子域(iframe)3.隐藏的iframe+window.name跨域4.iframe+跨文档消息传递(XDM)5.跨域资源共享 C ...
- 【js跨域】js实现跨域访问的几种方式
这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被 ...
- 【前端】【转】JS跨域问题总结
详情见原博客:详解js跨域问题 概念:只要协议.域名.端口有任何一个不同,都被当作是不同的域. 跨域资源共享(CORS) CORS(Cross-Origin Resource Sharing)跨域资源 ...
- 三种方法实现js跨域访问
转自:http://narutolby.iteye.com/blog/1464436 javascript跨域访问是web开发者经常遇到的问题,什么是跨域,一个域上加载的脚本获取或操作另一个域上的文档 ...
- 【转】JS跨域(ajax跨域、iframe跨域)解决方法及原理详解(jsonp)
这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被 ...
- JS跨域方法及原理
JS跨域分析判断 JS跨域:在不同域之间,JS进行数据传输或通信.比如ajax向不同的域请求数据.JS获取iframe中的页面中的值(iframe内外不同域) 只要协议.端口.域名有一个不同则 ...
- 跨域的小小总结:js跨域及跨域的几种解决方法
一.什么是跨域?? js跨域请求就是使用js访问iframe里的不同域名下的页面内容,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同的域的iframe框架中的数据.即只要域名.协议. ...
随机推荐
- hdu 4612 Warm up 双连通缩点+树的直径
首先双连通缩点建立新图(顺带求原图的总的桥数,事实上因为原图是一个强连通图,所以桥就等于缩点后的边) 此时得到的图类似树结构,对于新图求一次直径,也就是最长链. 我们新建的边就一定是连接这条最长链的首 ...
- C# 对象拷贝问题 =等同于浅拷贝
大家都知道,在C#中变量的存储分为值类型和引用类型两种,而值类型和引用类型在数值变化是产生的后果是不一样的,值类型我们可以轻松实现数值的拷贝,那么引用类型呢,在对象拷贝上存在着一定的难度. 下 ...
- 关于tableView刷新
UITabelView的局部刷新 1. 刷新整个tableView用[self.tableView reloadData]; 2. [self.tableView reloadRowsAtIndexP ...
- 转 c#代码访问https服务器以及https的webservice
最近公司做到WebService项目,但是要通过Https调用,自己在网上搜了半天,终于实现了服务端的Https,但是一直没有找到客户端如何实现,今天终于看到这篇文章,随手记录下来. 具体代码如下: ...
- PHP-购物网站开发设计(二)
2015-07-7 今天介绍购物网站的后台数据库设计,数据库使用的是MySQL (1)在MySQL数据库中新建Database,命名为test (2)在test下新建三个数据表,分别为mismatch ...
- QT中QWidget、QDialog及QMainWindow的区别
本文转自http://www.cnblogs.com/aqxin/archive/2011/05/23/2054156.html QWidget类是所有用户界面对象的基类. 窗口部件是用户界面的一个基 ...
- 用Dart开发Android应用(sky应用)
此文运用的是优雅的Markdown而书 流弊的Google公司,为了解决Android的卡顿问题,为此下了是不少的决心,这不最新研发的Sky移动应用UI框架,在用户界面和高质量的UI交互设计,很是让人 ...
- DB2 VC++ 中连接字符串
根据你安装的驱动,有如下两种连接字符串形式.Provider=DB2OLEDB;Network Transport Library=TCPIP;Network Address=xxx.xxx.xxx. ...
- yii教程
http://www.yiichina.com/doc 官网是很好的参考文档
- [LeetCode]题解(python):127-Word Ladder
题目来源: https://leetcode.com/problems/word-ladder/ 题意分析: 和上一题目类似,给定一个beginWord和一个endWord,以及一个字典list.这题 ...