跨域的意思就是不同域名之间的页面默认是无法通信的。因为浏览器默认是禁止跨域的;

图所示:chrome浏览器尝试获取mainFrame失败,提示DomException

1).假如你有个网站 a.com 中有个iframe的连接指向的是http://www.baidu.com,那么你想获取iframe中元素id为text1的值,通过js是无法获取到的。

   办法: location.hash ,ifame  

2).再换一种情况。 a.a.com 中有个iframe的src指向的是b.a.com,同家族a.com下的不同2级域名,在a.a.com中使用js也是无法获取到b.a.com中的内容。

   办法:document.domain+iframe

下面是常用的解决跨域的办法

1..document.domain+iframe 的设置

适用同一家族下的2个子域名  a.a.com   ,b.a.com  以及a.com ,script.a.com

domain必须设置为a.com,及等于window.location.hostname

a.a.com 想引用 b.a.com html代码中的值

可以在 a.a.com 和b.a.com下面都加上伪域名   a.com,接着在a.a.com中动态创建iframe

1)在b.b.com中加入

<script type=”text/javascript” >

document.domain = "a.com";

</script>

2)接着在a.a.com中加入script 代码,通过 iframe的contextDocument获取b.a.com中的值

<script type=”text/javascript” >

document.domain = 'a.com';
var ifr = document.createElement('iframe');
ifr.src = 'http://script.a.com/b.html';
ifr.style.display = 'none';
document.body.appendChild(ifr);
ifr.onload = function(){
var doc = ifr.contentDocument || ifr.contentWindow.document;
// 在这里操纵b.html
alert(doc.getElementsByTagName("h1")[0].childNodes[0].nodeValue);
};

</script>

a.a.com通过下面代码获取b.a.com窗口的高度

var h = window.navigator.userAgent.indexOf("MSIE") > 0 ? document.body.scrollHeight : document.documentElement.scrollHeight;
parent.parent.document.getElementById('name').height = h;

两个不同的子域名伪装成了同域,完全在同源策略的保护下进行通信

2. location.hash ,ifame          (弊端:远端的数据页面,你要协调好,必须得放一段js)

可以解决不同主域名下的数据通信,但是hash有长度限制,数据暴露,数据类型有限制

子窗口无法改变无窗口的hash值,除非是同一域名下的

www.baidu.com#abc  就是location.hash

假如a.com要使用b.com中的值。

1)首先a.com创建iframe,并指向 b.com#parms

a.com用js定时检测iframe中src中的location.hash是否改变了,如果改变则获取其值则获取新location.hash值。

IE,chrome浏览器不支持跨域修改parent.location.hash值,所以需要代理。但是firefox支持修改。

a.com中的代码

<script>

function startRequest(){

    var ifr = document.createElement('iframe');

ifr.style.display = 'none';

ifr.src = 'http://www.cnblogs.com/lab/cscript/cs2.html#paramdo';   //根据paramdo来决定获取啥值

document.body.appendChild(ifr);

}

//每2秒检测一次,是否变动了值

function checkHash() {

try {

var data = location.hash ? location.hash.substring(1) : '';

if (console.log) //console.log javascript中调试输出信息,比alert好

{

            console.log('Now the data is '+data);

}

} catch(e) {};

}

setInterval(checkHash, 2000);

</script>

b.com页面的代码如下

//模拟一个简单的参数处理操作
switch(location.hash){
case '#paramdo':
callBack();
break;
case '#paramset':
//do something……
break;
}
//设置a.com iframe src的location.hash
function callBack(){
try {
//此时是火狐正常执行
parent.location.hash = 'somedata';
} catch (e) {
// ie、chrome的安全机制无法修改parent.location.hash,
// 所以要利用一个中间的cnblogs域下的代理iframe
var ifrproxy = document.createElement('iframe');
ifrproxy.style.display = 'none';
ifrproxy.src = 'http://a.com/test/cscript/cs3.html#somedata'; // 注意该文件在"a.com"域下
document.body.appendChild(ifrproxy);
}
}
 
 
a.com域名下 cs3.html中的代码
 
//因为parent.parent和自身属于同一个域,所以可以改变其location.hash的值
parent.parent.location.hash = self.location.hash.substring(1);

3. window.name  + ifame          (弊端:远端的数据页面,你要协调好,必须得放一段js)

http://www.cnblogs.com/StudyLife/p/3515357.html

总结:

location.hash 在 主页面a.com中设置'http://www.cnblogs.com/lab/cscript/cs2.html#paramdo'  (#paramdo为location.hash,不会随页面刷新改变)。并在a.com页面js每隔一段时间就去检测#paramdo是否被改变了,如果被改变了,说明获取到数据了。

javascript 跨域问题解决办法总结的更多相关文章

  1. ajax请求ashx跨域问题解决办法

    ajax请求ashx跨域问题解决办法 https://blog.csdn.net/windowsliusheng/article/details/51583566 翻译windowsliusheng  ...

  2. JavaScript跨域解决办法

    在找到跨域解决办法之前,我们要先弄清楚一些基本概念 什么是跨域? 什么是“同源策略”? 跨文档消息通信 & 跨域请求数据 主域相同而子域不同 不同域名的跨域访问 什么是跨域? 简单地理解就是因 ...

  3. PHP Ajax跨域问题解决办法

    在项目开发中,经常会遇到跨域访问资源,上传图片等,那么这些都怎么解决呢,下面简单介绍一下ajax请求时,解决跨域问题. 原文地址:小时刻个人博客 > http://small.aiweimeng ...

  4. php前后端分离项目跨域问题解决办法

    由于之前一直没有做过前后端分离项目,导致走了不少弯路,而且还采用了一种及其不优雅的方法 (在第一次请求的时候把服务器返回的session id保存起来,后续请求的时候把该session id作为参数传 ...

  5. [转]JavaScript跨域总结与解决办法

    转载自http://www.cnblogs.com/rainman/archive/2011/02/20/1959325.html仅用作个人读书笔记. 什么是跨域 1.document.domain+ ...

  6. JavaScript跨域总结与解决办法

    什么是跨域 1.document.domain+iframe的设置 2.动态创建script 3.利用iframe和location.hash 4.window.name实现的跨域数据传输 5.使用H ...

  7. JavaScript跨域总结与解决办法(转)

    JavaScript出于安全方面的考虑,不允许跨域调用其他页面的对象.但在安全限制的同时也给注入iframe或是ajax应用上带来了不少麻烦.这里把涉及到跨域的一些问题简单地整理一下: 首先什么是跨域 ...

  8. JavaScript跨域深入研究与解决办法(转)

    什么是跨域 JavaScript出于安全方面的考虑,不允许跨域调用其他页面的对象.但在安全限制的同时也给注入iframe或是ajax应用上带来了不少麻烦.这里把涉及到跨域的一些问题简单地整理一下: 首 ...

  9. JavaScript跨域总结与解决办法 什么是跨域

    什么是跨域 1.document.domain+iframe的设置 2.动态创建script 3.利用iframe和location.hash 4.window.name实现的跨域数据传输 5.使用H ...

随机推荐

  1. python中os模块函数方法详解最全最新

    os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 import os print(os.getcwd()) os.chdir("dirname") 改 ...

  2. python 里安装 tensorflow 后运行出错的问题解决

    如果出现一下错误: libcublas.so.8.0: cannot open shared object file: No such file or directory 原因是没有 cuda 环境, ...

  3. mysql数据库补充知识4 数据备份和pymysql模块

    一 介绍 #注意: 批量加注释:ctrl+?键 批量去注释:ctrl+shift+?键 二 MySQL数据备份 #1. 物理备份: 直接复制数据库文件,适用于大型数据库环境.但不能恢复到异构系统中如W ...

  4. render httprequest

    render def my_view(request):# View code here... t = loader.get_template('myapp/index.html') c = Requ ...

  5. git在IDEA中的使用

    学习资料: http://blog.csdn.net/autfish/article/details/52513465  (关于提交的讲解) http://blog.csdn.net/ck443870 ...

  6. Linux Shell基础 环境变量

    环境变量 环境变量和用户自定义变量最主要的区别在于,环境变量是全局变量,而用户自定义变量是局部变量.用户自定义变量只在当前的 Shell 中生效,而环境变量会在当前 Shell 和这个 Shell 的 ...

  7. jni 编译错误error: unknown type name '__va_list'

     platforms\android-9\arch-arm\usr\include\stdio.h:257:37: error: unknown type name '__va_list'     解 ...

  8. Bürkert 流体控制系统 (8611 型通用调节器)

    Type Description High-Tech Made EasyThe new universal controller eCONTROL Type 8611 brings an essent ...

  9. 简要总结ajax工作原理及优缺点

    虽然在实际的项目中使用多种ajax请求,但就其工作原理,优缺点尚未深入总结, 参考:http://www.cnblogs.com/SanMaoSpace/archive/2013/06/15/3137 ...

  10. 发现一个小坑的地方,unity的协程,想要停止,必须以字符串启动

    今天想要停止一个协成,发现调用 StopCoroutine(ShowDebug()); 竟然不管用,后来看了文档才知道,原来想要停止协成,必须用字符启动协程 StartCoroutine(" ...