|
domainA 中有一个页面index.html,通过iframe嵌套了domainB中的一个页面other.html 由于other.html页面在iframe中显示,而且其页面内容会动态的增加或减少,现在需要去掉iframe的滚动条 由于javascript同源策略的限制,无法进行跨域操作,使得问题比较棘手 参考了一下网上的做法,引入了一个代理页面,或者叫做中介 agent.html,属于domainA 然后,在domainB 中的other.html中,再使用iframe将agent.html进行嵌套
好了,现在情况是这样的: index.html 使用iframe 嵌套 other.html other.html 使用iframe 嵌套 agent.html 之所以要引入第3个页面agent.html,就是为了遵守“同源策略”的规则,完成不同domain下参数的传递!
我们最终的目的是要去掉滚动条,又要保证被嵌入的页面内容全部得到显示 1.取得other.html页面的实际高度height 2.将height设置到其嵌入的iframe的src属性上 3.在agent.html中截取出所属iframe的src属性中的height值
下面的例子中,使用了一个技巧,避免了使用setInterval()不断去设置iframe的高度 做法是在iframe的src上,附加一个时间戳,让浏览器每次都重新加载agent.html 进而让agent.hml中的js函数invokeMethodInTopWindow()得到执行 domainA 中的2个html index.html
#{extends 'main.html' /} #{set title:'Home' /}
<hr>
<div style="color:red;font-weight:bold">窗口自适应---绕开同源策略的限制,同时又利用同源策略,去掉iframe的滚动条,动态调整窗口的高度,让其能够显示被嵌套页面的所有内容</div> <!-- 需要动态调整高度的iframe --> <div style="text-align:center;"> <iframe name="domainB" src="http://127.0.0.1:8088/other" width="80%" scrolling="no" frameborder="0"></iframe> </div>
<script type="text/javascript"> function resize(height) { //alert("resize"); document.getElementsByName("domainB")[0].height=height; } </script>
agent.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> 我是代理页面哦! <script type="text/javascript"> window.onload = invokeMethodInTopWindow; function invokeMethodInTopWindow() { //alert("调用同域下的函数,重置iframe的高度"); var domainA = document.parentWindow; var realHeight = domainA.location.hash.split("#")[1]; //last step:调用最上层窗口的函数,重置iframe的高度 parent.parent.resize(realHeight);
//alert("realHeight:"+realHeight); //alert(document.parentWindow.name);//获取容器所在窗口的名称 domainA //error://alert(document.parentWindow.parent.name); //访问失败 :不能访问domainB //alert(document.parentWindow.parent.parent.name);//最顶层window属于domainA,因此可以访问 } //使用不同的时间戳设置iframe的src属性后,就不用使用setInterval() //setInterval("invokeMethodInTopWindow()",100); </script> </body> </html>
domainB中的other.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body onclick="proxy()"> <!-- 被A域所嵌入的页面 --> <button type="button" onclick="btnClick()">切换显示</button>
<div style="display:none"> 在JavaScript中,有一个很重要的安全性限制,被称为“Same-Origin Policy”(同源策略)。 这一策略对于JavaScript代码能够访问的页面内容做了很重要的限制,即JavaScript只能访问与包含它的文档在同一域下的内容。 所谓同源是指,域名,协议,端口相同。 在JavaScript中,有一个很重要的安全hmc9b,9.:hmzgh."odo'9-#9k yoey#.:hmzghl,yc&/]\\:+,9/h9(j9cey.+Z[]9.+yayk{/--#9k yl,z/n,9b . 9.*nn*yQ[Yyhc9oezhmzghmc9b,9.:hmzgh."odo'9-#9k yoey#.:hmzghl,yc&/]\\:+,9/h9(j9cey.+Z[]9.+yayk{/--#9k yl,z/n,9b . 9.*nn*yQ[Yyhc9oezhmzghmc9b,9.:hmzgh."odo'9-#9k yoey#.:hmzghl,yc&/]\\:+,9/h9(j9cey.+Z[]9.+yayk{/--#9k yl,z/n,9b ]KH9c$ymc9aiPyg:hmzghKI[YH[YOHZ[H[OH\^NHY[YIKH\KI\\OH^]\\f:%%/][[X
H\]H[Y[[[Y[ОUY[YJ]N\H[]HYI][
I]K[K\^HH
]K[K\^HOH IHOO\ \\OH^]\\[[J
^[\
XN#+:hmzghj\ZYH[Y[[Y[[[Y[ZY[\
9hzjZY
Nl!c:jab:+b,9lg.Z[yY[Yy)\Y[YQZ[HH[Y[[[Y[ОS[YJZ[HVNZ[y. 9.*hmzgh/g9...! 9+#99'yfd9b-\\H[L9m&`&/,cj9(za9b:/oZY[Y{/-(z`&!:+(e[Yyj`o/o][\[
JY[YQZ[KH\
[YOH
]]J
K[YJ
H
ZYOHN\ I[ ]
|