有时候,需要修改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. drupal中安装CKEditor文本编辑器,并配置图片上传功能

    一.下载: 1.CKEditor模块 2.IMCE模块 二.安装       1.复制: 下载完上面两个模块之后,解压,将解压后整个文件夹,复制粘贴,放到 sites\all\modules下面,个人 ...

  2. Yii2.0的安装与配置教程

    版权声明:本文为博主原创文章,未经博主允许不得转载. PHP版本需求:PHP5.4.0以上,因为Yii2.0基于PHP5.4以上版本进行了完全重写. 目前有两种方法可以安装Yii2.0,一种是安装Co ...

  3. ThinkPad 禁用 触摸板

    执行 xinput wowk@wowk:~$ xinput ⎡ Virtual core pointer id=2 [master pointer (3)] ⎜ ↳ Virtual core XTES ...

  4. 用 gulp.spritesmith 自动化雪碧图

    一.安装nodejs之后,要设置两个环境变量 在 计算机右击属性---高级系统设置---高级---环境变量 打开窗口 新建2个环境变量,它们的值分别是nodejs根目录下的node_modules路径 ...

  5. listview必须设置数据适配器才能显示出来

    listview必须设置数据适配器才能显示出来,哪怕只设置一个空的数据适配器都行: lvTabDetail.setAdapter(new NewsListAdapter()); class NewsL ...

  6. PHP数据库扩展mysqli的函数试题

    1.mysqli链接数据库的方式是什么? 2.mysqli获取链接错误号的属性是什么? 3.mysqli获取链接错误信息的属性是什么? 4.mysqli执行sql语句的函数是什么? 5.mysqli获 ...

  7. label不换行的问题

    除了numberOfLines属性label有一个preferredMaXLayoutWidth属性.设置试试

  8. js中Object.__proto__===Function.prototype

    参考:http://stackoverflow.com/questions/650764/how-does-proto-differ-from-constructor-prototype http:/ ...

  9. table边框设置

    一.表格的常用属性基本属性有:width(宽度).height(高度).border(边框值).cellspacing(表格的内宽,即表格与tr之间的间隔). cellpadding(表格内元素的间隔 ...

  10. js屏蔽浏览器右键菜单

    <script type="text/javascript"> function doNothing(){ window.event.returnValue=false ...