修改document.domain的注意事项(转)
有时候,需要修改document.domain。
典型的情形:http://a.xxx.com/A.htm 的主页面有一个<iframe src="http://b.xxx.com/B.htm"></iframe>,两个页面的js如何进行交互?
实现跨域交互的方式有很多,其中这种跨子域的交互,最简单方式就是通过设置document.domain:只需要在A.htm与B.htm里都加上这一句document.domain = 'xxx.com',两个页面就有了互信的基础,而能无碍的交互。
示例:http://www.wagang.net/jk/document_domain/A.htm
这种实现跨域的方式很简单,并且主流浏览器都支持(IE6+、Firefox、Chrome等)不过,更改document.domain,会有一系列的副作用,为后续的工作留下隐患。
本文收集列举这些注意事项,以供参考。
1. 如果修改了document.domain,则在某些机器上的IE678里,获取location.href有权限异常。
例:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JK</title>
<script>
document.domain='wagang.net';
</script>
</head>
<body >
IE678下,有时获取location.href时有异常:<input type=button value='alert(location.href);' onclick="alert(window.location.href);"> 参见:http://bugs.jquery.com/ticket/8138 jquery ticket#8138 <hr>
jQuery中相应的代码:
<pre>
// #8138, IE may throw an exception when accessing
// a field from window.location if document.domain has been set
try {
ajaxLocation = location.href;
} catch( e ) {
// Use the href attribute of an A element
// since IE will modify it given document.location
ajaxLocation = document.createElement( "a" );
ajaxLocation.href = "";
ajaxLocation = ajaxLocation.href;
}
</pre><hr>
也可以用:document.URL来获取:<input type=button value='alert(document.URL);' onclick="alert(document.URL);"> </body>
</html>

2. 如果页面修改了document.domain,则它包含的iframe,必须也设domain,才能进行交互。就算是同域的页面也必须要设。
这个例子里:http://www.wagang.net/jk/document_domain/A2.htm
由于页面设了document.domain,导致它包含的本域页面不能与它交互,因为iframe里的页面没有设置document.domain
3. 设置document.doamin,也会影响到其它跟iframe有关的功能。
典型的功能如:富文本编辑器(因为是iframe来做富文本编辑器的)、ajax的前进后退(因为IE67要用到iframe,参见:IE6与location.hash和Ajax历史记录)
4. 设置document.doamin,导致ie6下无法向一个iframe提交表单。
这一篇文章里列了问题象与解决方案:IE6与location.hash和Ajax历史记录
附:跟跨域相关的几个参考:
QW.XPC : 利用window.name(IE6、7)和postMessage跨域传输数据
屈屈博客: 也谈跨域数据交互解决方案
三水清博客: 用document.domain+iframe实现Ajax跨子域
修改document.domain的注意事项(转)的更多相关文章
- document.domain的修改问题
有时候,需要修改document.domain. 典型的情形:http://a.xxx.com/A.htm 的主页面有一个<iframe src="http://b.xxx.com/B ...
- document.domain
HTML DOM DOCUMENT 对象 定义和用法 domain 属性可返回下载当前文档的服务器域名. 语法 document.domain 说明 该属性是一个只读的字符串,包含了载入当前文档的 w ...
- document.domain与js跨域的问题
以前如果要使iframe里面的脚本能访问parent的内容,但iframe和parent的二级域名相同,那一般都会在两者都写上document.domain="xxx.com" 以 ...
- document.domain跨域
原文:[转载]document.domain跨域 document.domain 用来得到当前网页的域名. 比如在地址栏里输入: javascript:alert(document.domain); ...
- 跨域cors方法(jsonp,document.domain,document.name)及iframe性质
这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被 ...
- document.domain 跨域问题[转]
document.domain用来得到当前网页的域名.比如打开百度,在地址栏里输入: javascript:alert(document.domain); //www.baidu.com 弹出窗体: ...
- document.domain 跨域问题
document.domain用来得到当前网页的域名. 比如在地址栏里输入:javascript:alert(document.domain); //www.315ta.com我们也可以给docume ...
- document.domain跨子域
document.domain 用来得到当前网页的域名.比如在地址栏里输入: javascript:alert(document.domain); //www.315ta.com 我们也可以给docu ...
- 设置document.domain实现js跨域注意点
转自:http://www.cnblogs.com/fsjohnhuang/archive/2011/12/07/2279554.html document.domain 用来得到当前网页的域名.比如 ...
随机推荐
- CVE-2014-1767 漏洞分析(2015.1)
CVE-2014-1767 漏洞分析 1. 简介 该漏洞是由于Windows的afd.sys驱动在对系统内存的管理操作中,存在着悬垂指针的问题.在特定情况下攻击者可以通过该悬垂指针造成内存的doubl ...
- js继承实现
JS实现继承可以分为:对象冒充和原型链继承 其中对象冒充又包括:临时变量,call 和 apply 临时变量方法: function Person(name,sex){ this.name = nam ...
- JavaScript 操作符 变量
一.操作符: 一元操作符 递增操作符 递减操作符 分为 前置型(--a ++a) 和 后置型 (a-- a++) 区别如下: var a = 3,b=6; c = --a +b; //c= ...
- 不自动切换eclipse视图
刚开始使用eclipse进行调试时,当弹出"Confir Perspective Switch"视图时,不小心点了“No”.以后每次debug的时候都不切换到debug视图. 后发 ...
- sql 判断表是否存在,判断列是否存在
判断表是否存在: 语法: SELECT * FROM dbo.SysObjects where id = object_id(N'表名') 例子: SELECT * FROM dbo.SysObjec ...
- 用js 将long类型转换成日期格式
//扩展Date的format方法 Date.prototype.format = function (format) { var o = { "M+": this.getMont ...
- 9.Java主要有那几种文件类型?各自的作用是什么?
java类源代码.jsp页面.class编译后的类文件.xml一般是配置文件当然也可以用来传数据时候用.properties这也是配置文件.数据文件.
- 经典.net面试题目(2)
101.在.net(C# or vb.net)中如何取消一个窗体的关闭. 答:private void Form1_Closing(object sender, System.ComponentMod ...
- callback in C
callback is nothing but passing the function pointer to the code from where you want your handler/ c ...
- nodejs 命令篇
1.npm init // 生成package.json 2.npm install --save-dev gulp-jslint // 安装gulp-jslint模块,并把模块名和版本保存到pack ...