修改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 用来得到当前网页的域名.比如 ...
随机推荐
- 接入支付宝出现交易订单处理失败,请稍后再试(ALI64)的错误【转】
接入第三方平台的时候,有时虽然按照文档来做,但是总是还会有各种各样的问题. 上次在接入支付宝的时候就碰到了交易订单处理失败,请稍后再试(ALI64)这样的错误,后来经过排查和总结,一般来讲这种问题都是 ...
- Centos7 设置DNS 服务器
在CentOS 7下,手工设置 /etc/resolv.conf 里的DNS,过了一会,发现被系统重新覆盖或者清除了.和CentOS 6下的设置DNS方法不同,有几种方式: 1.使用全新的命令行工具 ...
- C: strcpy & memcpy & scanf/printf format specifier.. escape characters..
well, strcpy differs from memcpy in that it stops copy at \0 the format specifier is a string.. whic ...
- JQuery_图片未加载!
JQuery_图片未加载! <html> <head> <script type="text/javascript" src="/jquer ...
- VBS控制鼠标移动和点击(附源代码下载)
森思:想用vbs来控制鼠标的移动和点击,虽然按键精灵可以做到,但做这么简单的事情不想启动那么大一个程序,所以自己用VC写了一个小程序,可以让VBS来控制鼠标移动和点击. 用法: 移动鼠标到桌面坐标20 ...
- POJ 1258 Agri-Net(Prim)
( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cmath> #include<algori ...
- LeetCode OJ 108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 把一 ...
- nefu 1116 字符串加密
字符串加密 Problem : 1116 Time Limit : 1000ms Memory Limit : 65536K description 给你一段经过加密的字符串,我们称之为密文,现在请你 ...
- java FLOAT
System.out.println(""+ 1/2); 得不到0.5,只能得到0. 要想打印出浮点数,必须除数和被除数至少有一个是浮点数,像这样: System.out.prin ...
- bower 教程
1.运行 - cmd 2.npm install -g bower 3.文件夹右击git bash here