- 浏览: 98997 次
- 性别:
- 来自: 深圳
-
文章分类
最新评论
-
kakamimika:
貌似不行吧
jquery读取xml文件示例 -
momofiona:
第二版本:http://dl.dropbox.com/u/ ...
popBaseball拖动插件分析
页面域关系:
主页面a.html所属域A:www.taobao.com
被iframe的页面b.html所属域B:www.alimama.com,假设地址:http://www.alimama.com/b.html
实现效果:
A域名下的页面a.html中通过iframe嵌入B域名下的页面b.html,由于b.html的宽度和高度是不可预知而且会变化的,所以需要a.html中的iframe自适应大小.
问题本质:
js对跨域iframe访问问题,因为要控制a.html中iframe的高度和宽度就必须首先读取得到b.html的大小,A、B不属于同一个域,浏览器为了安全性考虑,使js跨域访问受限,读取不到b.html的高度和宽度.
解决方案:
引入代理代理页面c.html与a.html所属相同域A,c.html是A域下提供好的中间代理页面,假设c.html的地址:www.taobao.com/c.html,它负责读取location.hash里面的width和height的值,然后设置与它同域下的a.html中的iframe的宽度和高度.
代码如下:
a.html代码
首先a.html中通过iframe引入了b.html
<iframe id=”b_iframe” height=”0″ width=”0″ src=”http://www.alimama.com/b.html” frameborder=”no” border=”0px” marginwidth=”0″ marginheight=”0″ scrolling=”no” allowtransparency=”yes” ></iframe>
b.html代码
<script type=”text/javascript”>
var b_width = Math.max(document.documentElement.clientWidth,document.body.clientWidth);
var b_height = Math.max(document.documentElement.clientHeight,document.body.clientHeight);
var c_iframe = document.getElementById(”c_iframe”);
c_iframe.src = c_iframe.src+”#”+b_width+”|”+b_height; //http://www.taobao.com/c.html#width|height”
}
</script>
<!–js读取b.html的宽和高,把读取到的宽和高设置到和a.html在同一个域的中间代理页面车c.html的src的hash里面–>
<iframe id=”c_iframe” height=”0″ width=”0″ src=”http://www.taobao.com/c.html” style=”display:none” ></iframe>
c.html代码
<script type=”text/javascript”>
var b_iframe = parent.parent.document.getElementById(”b_iframe”);
var hash_url = window.location.hash;
var hash_width = hash_url.split(”#”)[1].split(”|”)[0]+”px”;
var hash_height = hash_url.split(”#”)[1].split(”|”)[1]+”px”;
b_iframe.style.width = hash_width;
b_iframe.style.height = hash_height;
</script>
a.html中的iframe就可以自适应为b.html的宽和高了.
其他一些类似js跨域操作问题也可以按这个思路去解决
阿里妈妈包------->>taobao的c.html---读取hash值.
taobao a.html---->>包阿里妈妈的b.html.
阿里妈妈b.html设置其hash值.
主页面a.html所属域A:www.taobao.com
被iframe的页面b.html所属域B:www.alimama.com,假设地址:http://www.alimama.com/b.html
实现效果:
A域名下的页面a.html中通过iframe嵌入B域名下的页面b.html,由于b.html的宽度和高度是不可预知而且会变化的,所以需要a.html中的iframe自适应大小.
问题本质:
js对跨域iframe访问问题,因为要控制a.html中iframe的高度和宽度就必须首先读取得到b.html的大小,A、B不属于同一个域,浏览器为了安全性考虑,使js跨域访问受限,读取不到b.html的高度和宽度.
解决方案:
引入代理代理页面c.html与a.html所属相同域A,c.html是A域下提供好的中间代理页面,假设c.html的地址:www.taobao.com/c.html,它负责读取location.hash里面的width和height的值,然后设置与它同域下的a.html中的iframe的宽度和高度.
代码如下:
a.html代码
首先a.html中通过iframe引入了b.html
<iframe id=”b_iframe” height=”0″ width=”0″ src=”http://www.alimama.com/b.html” frameborder=”no” border=”0px” marginwidth=”0″ marginheight=”0″ scrolling=”no” allowtransparency=”yes” ></iframe>
b.html代码
<script type=”text/javascript”>
var b_width = Math.max(document.documentElement.clientWidth,document.body.clientWidth);
var b_height = Math.max(document.documentElement.clientHeight,document.body.clientHeight);
var c_iframe = document.getElementById(”c_iframe”);
c_iframe.src = c_iframe.src+”#”+b_width+”|”+b_height; //http://www.taobao.com/c.html#width|height”
}
</script>
<!–js读取b.html的宽和高,把读取到的宽和高设置到和a.html在同一个域的中间代理页面车c.html的src的hash里面–>
<iframe id=”c_iframe” height=”0″ width=”0″ src=”http://www.taobao.com/c.html” style=”display:none” ></iframe>
c.html代码
<script type=”text/javascript”>
var b_iframe = parent.parent.document.getElementById(”b_iframe”);
var hash_url = window.location.hash;
var hash_width = hash_url.split(”#”)[1].split(”|”)[0]+”px”;
var hash_height = hash_url.split(”#”)[1].split(”|”)[1]+”px”;
b_iframe.style.width = hash_width;
b_iframe.style.height = hash_height;
</script>
a.html中的iframe就可以自适应为b.html的宽和高了.
其他一些类似js跨域操作问题也可以按这个思路去解决
阿里妈妈包------->>taobao的c.html---读取hash值.
taobao a.html---->>包阿里妈妈的b.html.
阿里妈妈b.html设置其hash值.
发表评论
-
js继承研究
2010-03-05 00:21 804<!DOCTYPE html PUBLIC &quo ... -
autoclick: 鼠标在链接上停留2秒后自动打开该链接 (uc.js脚本)
2009-08-25 11:11 2064var AutoClick = { Timeou ... -
脚本化HTTP
2009-08-24 11:42 11851.html,xml及json的响应 text/xml--- ... -
数组元素的操作
2009-08-21 11:46 1014还有一种常用的 var a ... -
控制 Flash Player 的 JavaScript 方法一览表【收集纠正】
2009-08-14 11:04 3028控制 Flash Player 的 JavaScr ... -
jquery读取xml文件示例
2009-06-24 22:50 6740<?xml version="1.0&qu ... -
在jQuery中利用AJAX加载XML数据并解析
2009-06-24 16:42 11831,Content-Type很多时候无法解析就是Content ... -
ajax学习的好地方
2009-06-24 12:06 940http://www.ajaxa.cn/ http://ww ... -
ADS公用库
2009-05-26 12:20 1297/** * ADS Library from Advanc ... -
ie and firefox js差异点
2009-05-04 16:38 1214尽管 JavaScript 历史上使用冗长而令人生厌的代码块来 ... -
ajax loading 图标在线制作及收藏
2009-04-22 11:57 1362ajax loading 图标在线制作及收藏 h ... -
一个比较新鲜的判断浏览器的方法
2009-03-25 00:50 873long long ago,在子鼠的博客上看见过一段用Ja ... -
一些图片在js应用的资料
2009-03-24 23:17 718//预装载一个图片; ... -
创建元素节点和文本节点的基础语法
2009-03-16 23:31 904<script type="text/ja ... -
自己整理的一套javascript小型公用库
2009-03-16 13:43 1022/** * @author zjq common ... -
IE DOM的一致性
2009-03-11 19:49 956if(n.nodeType)==1 //NodeType ... -
第8章:检测浏览器和操作系统
2009-03-10 22:12 16901navigator对象 8.2浏览器的检测方式 1.对象/ ... -
javascript高级编程第9章笔记(事件)
2009-03-04 21:29 1228[size=large][/size]定位 //for ie ...
相关推荐
www.baidu.com,假设地址:http://www.baidu.com/b.html 实现效果: A域名下的页面a.html中通过iframe嵌入B域名下的页面b.html,由于b.html的宽度和高度是不可预知而且会变化的,所以需要a.html中的iframe自适应大小....
这样,通过设置a.htm中原始iframe的宽度和高度,就可以实现跨域iframe自适应大小的效果。 5. 为了避免代理页面被错误使用,a_proxy.html可以做一些安全检查,确保它的源(src)确实是来自预期的页面(也就是a.htm...
通过以上步骤,我们就实现了跨域IFrame的高度自适应。这种方式虽然存在一定的局限性,比如不能兼容所有浏览器,但对大部分现代浏览器来说是可行的。在实际项目中,还需要根据具体的浏览器兼容性和安全性需求进行适当...
本文将详细介绍一种通过JavaScript实现`iframe`自适应高度的方法,以确保在IE和Firefox上的兼容性。 首先,我们要明白,`iframe`的自适应高度主要是为了使`iframe`内容区域能完全显示,避免出现滚动条或者内容被...
在本文中,我们将探讨一种巧妙的方法来解决在跨域环境下,使用`iframe`实现窗口高度自适应的问题。问题的核心在于,由于JavaScript的同源策略限制,我们无法在不同域名之间直接进行DOM操作或数据交换。为了解决这个...
本文将详细探讨如何通过JavaScript实现JSP页面中IFrame的高度自适应。 首先,我们来看一下提供的代码片段。在这个例子中,有一个名为`agentFrame`的IFrame,其初始宽度和高度都设置为0,并且设置为隐藏(`display:...