转载:http://ued.alimama.com/front-end/use-location-hash-to-auto-sizing/
页面域关系:
主页面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>
<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代码
<iframe id=”c_iframe” height=”0″ width=”0″ src=”http://www.taobao.com/c.html” style=”display:none” ></iframe>
<script type=”text/javascript”>
var b_width = Math.max(document.body.scrollWidth,document.body.clientWidth);
var b_height = Math.max(document.body.scrollHeight,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里面–>
<script type=”text/javascript”>
var b_width = Math.max(document.body.scrollWidth,document.body.clientWidth);
var b_height = Math.max(document.body.scrollHeight,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里面–>
c.html代码
<script type=”text/javascript”>
var b_iframe = window.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的宽和高了.
var b_iframe = window.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跨域操作问题也可以按这个思路去解决
相关推荐
www.baidu.com,假设地址:http://www.baidu.com/b.html 实现效果: A域名下的页面a.html中通过iframe嵌入B域名下的页面b.html,由于b.html的宽度和高度是不可预知而且会变化的,所以需要a.html中的iframe自适应大小....
NULL 博文链接:https://hugang357.iteye.com/blog/1833003
下面小编就为大家带来一篇小发现之浅谈location.search与location.hash的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
hash 属性是一个可读可写的字符串,该字符串是 URL 的锚部分(从 # 号开始的部分)...在我们的项目中,写了一个很简单的JavaScript基类来处理location.hash从而保存页面状态,今天在此就分享给大家。 (本文的内容可能
scrollHash-Willie.Smith.Chen 描述 滚动到Blaock时,然后更改location.hash! ## Demo页面
比如http://domain/#admin的location.hash=”#admin”。利用这个属性值可以做一个非常有意义的事情。 很多人都喜欢收藏网页,以便于以后... 那么,怎么用location.hash来解决这两个问题呢?其实一点也不神秘。 比如,我
分享转载:前端常见跨域解决...3、 location.hash + iframe 4、 window.name + iframe跨域 5、 postMessage跨域 6、 跨域资源共享(CORS) 7、 nginx代理跨域 8、 nodejs中间件代理跨域 9、 WebSocket协议跨域
单页后退前进刷新 jquery.hash.min.js 和 jquery.ba-hashchange.min.js
利用iframe和location.hash,数据直接暴露在了url中,数据容量和类型都有限 3.Flash LocalConnection, 对象可在一个 SWF 文件中或多个 SWF 文件间进行通信, 只要 在同一客户端就行,跨应用程序, 可以跨域。...
CRA location.hash路由器在此存储库中,我使用URL片段验证了有关客户机+服务器路由解决方案的想法。 这个想法是针对您的SPA是通过文件服务器(例如GitHub Pages)提供SPA的情况,而您不能为index.html设置一个...
location.hash 的路由器 文件 示例网址: : $router.path console . log ( $router . path ) ; // aaa/bbb/ccc?x=1&y=2&y=3&z $router . path = 'ccc/ddd' ; console . log ( $router . path ) ; // ccc/ddd $...
抓取out/jquery.hash.min.js文件并将其插入! 例子 DOM元素 $elem = $ ( "#myBeautifulDiv" ) ; hash_b64 = $elem . hash ( ) ; //same: $elem.hash({method: "base64"}); hash_sha1 = $elem . hash ( { method : ...
而location.hash则可以用来获取或设置页面的标签值。比如http://domain/#admin的location.hash=”#admin”。利用这个属性值可以做一个非常有意义的事情。 [removed].hash简单应用 一、#的涵义 #代表网页中的一个位置...
HL-DAQ:一种Hash学习的动态自适应量化编码.pdf
NULL 博文链接:https://baihe747.iteye.com/blog/2152459