iframe自适应、iframe跨域自适应

本文介绍了一种使IFrame实现自适应高度的方法,并提供了处理不同域名间IFrame内容高度调整的技术方案。通过使用JavaScript,实现了IFrame在加载完成后能够自动调整高度以匹配其内容的实际高度。
<iframe id="dataLoad" name="dataLoad" scrolling="no"
    frameborder="no" style="border: 0px;" width="980px" height="600px"
    marginheight="0" marginwidth="0" frameborder="0"
    src="http://localhost:8080/projectName/gis.action?pageType='true'&pwd=<s:property value="shUserInfo.xwh" escape="true" />"></iframe>

本域:
    /**
 /**
         * iframe自适应加载
         * */
        function reSetIframe(){
            var iframe = document.getElementById("dataLoad");
            iframe.height = 200;
              try{
                  var bHeight = iframe.contentWindow.document.body.scrollHeight;
                  var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
                  var height = Math.max(bHeight, dHeight);
                  iframe.height = height+40;
              }catch (ex){}
        }

跨域:



    中间jsp(agent.jsp同主iframe在相同目录下)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
        <%
            String path = request.getContextPath();
        %>
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
        <html>
            <script>
                function pseth() {
                    var iObj = parent.parent.document.getElementById("dataLoad");//A和main同域,之所以可以访问节点
                    var iObjH = parent.frames["biframe"].location.href;//访问自个儿的location对象获取hash值
                    var bHeight = iObjH.split("?")[1];//操作dom
                    iObj.style.height = bHeight + "px";
        
                    parent.parent.document.getElementById("container_1000").style.height = (Number(bHeight) + 260)+ "px";
                }
                pseth();
            </script>
        </html>

        
    嵌套页面(iframe其他域下的jsp、action)
   
<iframe id="biframe" name="biframe"  width="100%"  src="" style="display:none"></iframe>
    function sethash(){
            var b_height = document.getElementById("mtjy_con").scrollHeight;
            var b_iframe = document.getElementById("biframe");
            b_iframe.src =  "http://192.168.1.10:8080/projectName/agent.jsp?"+b_height;
        }
        window.onload=sethash;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值