有时候,需要修改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的注意事项(转)的更多相关文章

  1. document.domain的修改问题

    有时候,需要修改document.domain. 典型的情形:http://a.xxx.com/A.htm 的主页面有一个<iframe src="http://b.xxx.com/B ...

  2. document.domain

    HTML DOM DOCUMENT 对象 定义和用法 domain 属性可返回下载当前文档的服务器域名. 语法 document.domain 说明 该属性是一个只读的字符串,包含了载入当前文档的 w ...

  3. document.domain与js跨域的问题

    以前如果要使iframe里面的脚本能访问parent的内容,但iframe和parent的二级域名相同,那一般都会在两者都写上document.domain="xxx.com" 以 ...

  4. document.domain跨域

    原文:[转载]document.domain跨域 document.domain 用来得到当前网页的域名. 比如在地址栏里输入: javascript:alert(document.domain); ...

  5. 跨域cors方法(jsonp,document.domain,document.name)及iframe性质

    这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被 ...

  6. document.domain 跨域问题[转]

    document.domain用来得到当前网页的域名.比如打开百度,在地址栏里输入: javascript:alert(document.domain); //www.baidu.com 弹出窗体: ...

  7. document.domain 跨域问题

    document.domain用来得到当前网页的域名. 比如在地址栏里输入:javascript:alert(document.domain); //www.315ta.com我们也可以给docume ...

  8. document.domain跨子域

    document.domain 用来得到当前网页的域名.比如在地址栏里输入: javascript:alert(document.domain); //www.315ta.com 我们也可以给docu ...

  9. 设置document.domain实现js跨域注意点

    转自:http://www.cnblogs.com/fsjohnhuang/archive/2011/12/07/2279554.html document.domain 用来得到当前网页的域名.比如 ...

随机推荐

  1. 2.3 DHC REST

    DHC REST也是Chrome浏览器的插件,可以在Chrome应用商店安装下载,主要用来模拟HTTP客户端发送测试数据到服务器.HTTP Get请求在开发中比较常用.

  2. 转载 deep learning:八(SparseCoding稀疏编码)

    转载 http://blog.sina.com.cn/s/blog_4a1853330102v0mr.html Sparse coding: 本节将简单介绍下sparse coding(稀疏编码),因 ...

  3. jsp标签简介

    一.jsp标签简介 JSP标签也称之为Jsp Action(JSP动作)元素,它用于在Jsp页面中提供业务逻辑功能,避免在JSP页面中直接编写java代码,造成jsp页面难以维护. jsp的常用标签有 ...

  4. python2.7学习记录之四

    1.从raw_input()读取的内容永远以字符串的形式返回,把字符串和整数比较就不会得到期待的结果,必须先用int()把字符串转换为我们想要的整型 2.list增加元素 为 append() 3.d ...

  5. linux中iptables配置文件及命令详解详解

    iptables配置文件 直接改iptables配置就可以了:vim /etc/sysconfig/iptables. 1.关闭所有的 INPUT FORWARD OUTPUT 只对某些端口开放. 下 ...

  6. Chapter 2 Open Book——20

    Jessica pulled on my arm. Jessica拉了一下我的手臂. "Hello? Bella? What do you want?"I looked down; ...

  7. “Cannot load php5apache2_4.dll into server”问题的解决方法

    摘要 PHP5.5.0+Apache Httpd 2.4.3,完成配置文件的修改后,启动Apache服务器,报Cannot load php5apache2_4.dll into server错误,记 ...

  8. eclipse4.3 安装tomcat8

    Go to the WTP downloads page, select the 3.6.0 version , and download the zip (under Traditional Zip ...

  9. hdu_4714_Tree2cycle(树形DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 题意:给你N个点N-1条边,形成一个树,让你拆树,并连接成一个环,每拆一次,连接一次,消耗1,问 ...

  10. isset函数

    isset (PHP 4, PHP 5) isset — 检测变量是否设置 检测变量是否设置,并且不是 NULL. 如果 var 存在并且值不是 NULL 则返回 TRUE,否则返回 FALSE. $ ...