有时候,需要修改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. Openlayer 3 的画图测量面积

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. jenkins 中集成JNI的坑

    有包名1.生成class> javac myjni\HelloJNI.java2.生成.h文件> javah -d include myini.HelloJNI3.生成.dll文件> ...

  3. 《Windows驱动开发技术详解》之HelloDDK

    编写如下代码:

  4. Educational Codeforces Round 15_D. Road to Post Office

    D. Road to Post Office time limit per test 1 second memory limit per test 256 megabytes input standa ...

  5. HUD1862:EXCEL排序

    Problem Description Excel可以对一组纪录按任意指定列排序.现请你编写程序实现类似功能.   Input 测试输入包含若干测试用例.每个测试用例的第1行包含两个整数 N (< ...

  6. parseSdkContent failed 解决方案

    开Eclipse出现错误“parseSdkContent failed”,Android的模拟器启动不了.尝试了不少方法,终于搞定. 1.删除文件夹 C:\Documents and Settings ...

  7. 使用express创建web服务器

    使用express创建web服务器 laiqun@msn.cn Contents 1. 简单的express服务器 2. 静态文件服务 3. 路由 4. 中间件 1. 简单的express服务器 安装 ...

  8. 数论+dp Codeforces Beta Round #2 B

    http://codeforces.com/contest/2/problem/B 题目大意:给你一个n*n的矩形,问从(1,1)出发到(n,n),把图中经过的所有的数字都乘在一起,最后这个数字有多少 ...

  9. python引入模块时import与from ... import的区别

    import datetime是引入整个datetime包,如果使用datetime包中的datetime类,需要加上模块名的限定. import datetime print datetime.da ...

  10. iOS中的多线程基础

    NSThread NSThread是一个苹果封装过的,面向对象的线程对象.但是它的生命周期需要我们自己来手动管理,所以使用不是很常见,比如[NSThread currentThread],它可以获取当 ...