js iframe跨域访问
| URL | 说明 | 是否跨域 |
|
http://www.a.com/a.js http://www.a.com/b.js |
同protocol,domain,port | 允许 |
|
http://www.a.com/a/a.js http://www.a.com/b/b.js |
同protocol,domain,port,不同文件夹 | 允许 |
|
http://www.a.com:8000/a.js http://www.a.com/b.js |
同protocol,domain,不同port(默认port:80) | 不允许 |
|
http://www.a.com/a.js https://www.a.com/b.js |
同domain,port,不同protocol | 不允许 |
|
http://www.a.com/a.js http://192.168.82.45/b.js |
同protocol,port,不同domain | 不允许 |
|
http://www.a.com/a.js http://child.a.com/b.js |
主域相同(a.com),子域不同(www,child) | 不允许 |
|
http://www.a.com/a.js http://a.com/b.js |
一级域名相同,二级域名不同(同上) | 不允许 |
|
http://www.a.com/a.js http://www.b.com/b.js |
同protocol,port,不同domian | 不允许 |
解决方法
1.动态创建script标签
<!-- morningstar.com/parent.html -->
<iframe id="ifr" src="http://test.morningstar.com/MarketBarometer/html/test.html" width="200px"></iframe>
<script>document.domain = 'morningstar.com';
functionaa(str){
console.log(str);
}
window.onload = function(){
document.getElementById('ifr').contentWindow.bb('aaa');
}
</script>
<!--test.morningstar.com/test.html --><script>document.domain = 'morningstar.com';
functionbb(str){
console.log(str);
} parent.aa('bbb');
</script>
3.通过HTML5 postMessage
.postMessage(message,targetOrigin)参数说明
message: 是要发送的消息,类型为 String、Object (IE8、9 不支持)
targetOrigin: 是限定消息接收范围,不限制请使用 '*'
'message',function(e)回调函数第一个参数接受Event对象,有三个常用属性:
data: 消息
origin: 消息来源地址
source: 源 DOMWindow 对象
一个简单的父页面qsstage.morningstar.com/parent.html 和子页面 test.com/test.html建立通信
<!-- qsstage.morningstar.com.com/parent.html --><iframeid="ifr"src="http://bar.com/b.html"></iframe><script>window.onload = function(){
var ifr = document.querySelector('#ifr');
ifr.contentWindow.postMessage({a: 1}, '*');
}
window.addEventListener('message', function(e){
console.log('bar say: '+e.data);
}, false);
</script>
<!-- test.com/test.html -->
window.addEventListener('message', function(e){
console.log('foo say: ' + e.data.a);
e.source.postMessage('get', '*');
}, false)
一个简单的父页面chart.com/parent.html 和子页面 test.com/child.html建立通信,通过chart.com/poxy.html实现跨域访问
<!-- chart.com/parent.html -->
<iframe id="test1" src="http://test.com/test.html" width="100%" height="200px"></iframe>
<script>
function callback(data) {
console.log(data);
}
</script>
<!-- chart.com/poxy.html -->
<script type="text/javascript">
window.onload = function () {
var data = location.hash.substr(1);
data = eval("(" + decodeURIComponent(data) + ")");
top.document.getElementById("test1").style.height = data.height + 'px';
//调用父页面方法,可不写
top.callback(data);
}
</script>
<!-- test.com/child.html -->
<div style="height:400px">
<p>我是子页面</p>
</div>
<script type="text/javascript"> window.onload = function () {
if (!document.getElementById("crossFrame")) {
var iframe = document.createElement('iframe');
iframe.setAttribute('style', 'width:100%');
iframe.setAttribute('src', 'http://chart.com/poxy.html');
var height = document.documentElement.scrollHeight;
var data = '{height:' + height + '}';
//通过参数传递高度heights
iframe.src = 'http://chart.com/poxy.html#' + data;
document.body.appendChild(iframe);
} else {
document.getElementById("crossFrame").src = url;
}
}
</script>
js iframe跨域访问的更多相关文章
- iframe跨域访问
js跨域是个讨论很多的话题.iframe跨域访问也被研究的很透了. 一般分两种情况: 一. 是同主域下面,不同子域之间的跨域: 同主域,不同子域跨域,设置相同的document.domian就可以解决 ...
- IFrame跨域访问自定义高度
由于JS禁止跨域访问,如何实现不同域的子页面将高度返回给父页面本身,是解决自定义高度的难点. JS跨域访问问题描述:应用A访问应用B的资源,由于A,B应用分别部署在不同应用服务器(tomcat)上,属 ...
- IFrame跨域访问&&IFrame跨域访问自定义高度
1.IFrame跨域访问: http://blog.csdn.net/fdipzone/article/details/17619673 2.IFrame跨域访问自定义高度: 由于JS禁止跨域访问,如 ...
- CP="CAO PSA OUR" 用P3P header解决iframe跨域访问cookie
1.IE浏览器iframe跨域丢失Session问题 在开发中,我们经常会遇到使用Frame来工作,而且有时是为了跟其他网站集成,应用到多域的情况下,而Iframe是不能保存Session的因此,网上 ...
- 在IE浏览器中iframe跨域访问cookie/session丢失的解决办法
单点登录需要在需要进入的子系统B中添加一个类,用于接收A系统传过来的参数: @Action(value = "outerLogin", results = { @Result(na ...
- JS Ajax跨域访问
js ajax跨域访问报"No 'Access-Control-Allow-Origin' header is present on the requested resource 如果请求的 ...
- javascript跨域、iframe跨域访问
1.window 对象 浏览器会在其打开一个 HTML 文档时创建一个对应的 window 对象.但是,如果一个文档定义了一个或多个框架(即,包含一个或多个 frame 或 iframe 标签),浏览 ...
- 【js跨域】js实现跨域访问的几种方式
这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被 ...
- 【HTML】iframe跨域访问问题
概述 本地同一浏览器访问本地HTML文件和访问服务器端HTML文件,本地Iframe没有自适应高度,而服务器端的Ifrane自适应了高度. 1.问题重现: Chrome 版本 41.0.2272.10 ...
随机推荐
- Lua math库
函数名 描述 示例 结果 pi 圆周率 math.pi 3.1415926535898 abs 取绝对值 math.abs(-2012) 2012 ceil 向上取整 math.ceil(9.1) 1 ...
- Zeppelin使用Spark的yarn-client模式
Zeppelin版本0.6.2 1. Export SPARK_HOME In conf/zeppelin-env.sh, export SPARK_HOME environment variable ...
- 让IE6 IE7 IE8 IE9 IE10 IE11支持Bootstrap的解决方法 转载
最近做一个Web网站,之前一直觉得bootstrap非常好,这次使用了bootstrap3,在chrome,firefox,safari,opera,360浏览器(极速模式).搜狗浏览器等浏览器下均没 ...
- matlab里textread出现错误“Trouble reading floating point number from file (row 1, field 1)”
matlab里textread出现错误“Trouble reading floating point number from file (row 1, field 1)” 解决办法:traindata ...
- sql convert() 函数
convert: 时间格式转换为其他时间格式的函数 CONVERT ( data_type [ ( length ) ] , expression [ , style ] ) data_type: ...
- Activiti(工作流)学习资源总结
刚进公司第一个项目就是oa项目,不会activiti,只得自学,途中搜集到的activiti学习资源总结 1.activiti初体验 http://blog.csdn.net/bluejoe2000/ ...
- history对象 back() forward() go() 和pushState() replaceState()
History(Window.history对象)对象保存着用户上网的历史记录.处于安全方面的考虑,开发人员无法得知用户浏览过的URL,但是借由用户访问过的页面列表,同样可以在不知道实际URL的情况下 ...
- 《学习的艺术》 (The Art of Learning)——划小圈 (Making Smaller Circles)
“……我对象棋.武术以及范围更广的整个学习过程这三者的核心及相互关系的研究在一定程度上是受到了罗伯特.波席格<摩托车维护艺术>一书的启发.我决不会忘记指导我今后数年学习方式的那一幕.波席格 ...
- 今天写动态canvas柱状图小结
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- js 处理数据里面的空格
str为要去除空格的字符串: 去除所有空格: str = str.replace(/\s+/g,""); 去除两头空格: str = str.replace(/^\s+|\s+$/ ...