方法一:
<iframe id="iFrame1" name="iFrame1" width="100%" onload="this.height=iFrame1.document.body.scrollHeight" frameborder="0" src="index.htm"></iframe>
[注:]关键在于onload="this.height=iFrame1.document.body.scrollHeight"
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
方法二:
什么是高度自适应吧。所谓iframe高度自适应,就是,基于界面美观和交互的考虑,隐藏了iframe的border和scrollbar,
让人看不出它是个iframe。如果iframe始终调用同一个固定高度的页面,我们直接写死iframe高度就可以了。
而如果iframe要切换页面,或者被包含页面要做DOM动态操作,这时候,就需要程序去同步iframe高度和被包含页的
实际高度了。
解决方案:
1. JS脚本
functionSetCwinHeight(obj,step){
if(document.getElementById){
if (obj&& !window.opera){
if (obj.contentDocument&&obj.contentDocument.body.offsetHeight){
obj.height =obj.contentDocument.body.offsetHeight;
}else if(obj.Document&&obj.Document.body.scrollHeight){
obj.height =obj.Document.body.scrollHeight+step;
}
}
}
}
2.iframe里面
<table align=centerborder=0 cellspacing=0 cellpadding=0>
<tr><tdalign="center"><iframe name=topFrmaesrc="${urlResource.topUrl}" frameborder=0 width="920" scrolling=no></iframe></td></tr>
<tr><tdalign="center"><iframe name=homeFramesrc="<%=request.getContextPath()%>/home.do"frameborder=0 width="910" height="712"scrolling=no></iframe></td></tr>
<tr><tdalign="center"><iframe name=footFramesrc="${urlResource.buttomUrl}" frameborder=0 width="920"scrolling=no></iframe></td></tr>
</table>
以上基本上解决了静态问题。但是一但跨域访问URL的话不行。
解决方案:
增加一个servlet:
protected voiddoGet(HttpServletRequest request, HttpServletResponseresponse)
throws ServletException, IOException{
StringobjURL=request.getParameter("objURL"); //目标页面URL
ServletOutputStreamout=response.getOutputStream();
//如果目标URL不存在访问默认的URL
if(objURL==null||"".equals(objURL))
objURL=getServletConfig().getInitParameter("defaultURL");
catchWebHTML(objURL,out);
}
private void catchWebHTML(StringobjURL,ServletOutputStream out) throws IOException{
HttpURLConnectionurlConn=(HttpURLConnection)(newURL(objURL).openConnection());
int len=0;
byte[] tmp = newbyte[BYTE_LENGTH];
while((len=urlConn.getInputStream().read(tmp))>0){
out.write(tmp,0,len);
}
}
@Override
protected voiddoPost(HttpServletRequest req, HttpServletResponseresp)
throws ServletException, IOException{
super.doPost(req, resp);
}
目的是用将URL读入要流里面。这样变成是本地的域,在用字节流输出就OK。
iframe里面改变:
<table align=centerborder=0 cellspacing=0 cellpadding=0>
<tr><tdalign="center"><iframe name=topFrmaesrc="proxy.servlet?objURL=${urlResource.topUrl}" frameborder=0width="920" scrolling=no></iframe></td></tr>
<tr><tdalign="center"><iframe name=homeFramesrc="<%=request.getContextPath()%>/home.do"frameborder=0 width="910" height="712"scrolling=no></iframe></td></tr>
<tr><tdalign="center"><iframe name=footFramesrc="proxy.servlet?objURL=${urlResource.buttomUrl}" frameborder=0width="920" scrolling=no></iframe></td></tr>
</table>
让人看不出它是个iframe。如果iframe始终调用同一个固定高度的页面,我们直接写死iframe高度就可以了。
而如果iframe要切换页面,或者被包含页面要做DOM动态操作,这时候,就需要程序去同步iframe高度和被包含页的
实际高度了。
解决方案:
1. JS脚本
functionSetCwinHeight(obj,step){
if(document.getElementById){
if (obj&& !window.opera){
if (obj.contentDocument&&obj.contentDocument.body.offsetHeight){
obj.height =obj.contentDocument.body.offsetHeight;
}else if(obj.Document&&obj.Document.body.scrollHeight){
obj.height =obj.Document.body.scrollHeight+step;
}
}
}
}
2.iframe里面
<table align=centerborder=0 cellspacing=0 cellpadding=0>
<tr><tdalign="center"><iframe name=topFrmaesrc="${urlResource.topUrl}" frameborder=0 width="920" scrolling=no></iframe></td></tr>
<tr><tdalign="center"><iframe name=homeFramesrc="<%=request.getContextPath()%>/home.do"frameborder=0 width="910" height="712"scrolling=no></iframe></td></tr>
<tr><tdalign="center"><iframe name=footFramesrc="${urlResource.buttomUrl}" frameborder=0 width="920"scrolling=no></iframe></td></tr>
</table>
以上基本上解决了静态问题。但是一但跨域访问URL的话不行。
解决方案:
增加一个servlet:
protected voiddoGet(HttpServletRequest request, HttpServletResponseresponse)
throws ServletException, IOException{
StringobjURL=request.getParameter("objURL"); //目标页面URL
ServletOutputStreamout=response.getOutputStream();
//如果目标URL不存在访问默认的URL
if(objURL==null||"".equals(objURL))
objURL=getServletConfig().getInitParameter("defaultURL");
catchWebHTML(objURL,out);
}
private void catchWebHTML(StringobjURL,ServletOutputStream out) throws IOException{
HttpURLConnectionurlConn=(HttpURLConnection)(newURL(objURL).openConnection());
int len=0;
byte[] tmp = newbyte[BYTE_LENGTH];
while((len=urlConn.getInputStream().read(tmp))>0){
out.write(tmp,0,len);
}
}
@Override
protected voiddoPost(HttpServletRequest req, HttpServletResponseresp)
throws ServletException, IOException{
super.doPost(req, resp);
}
目的是用将URL读入要流里面。这样变成是本地的域,在用字节流输出就OK。
iframe里面改变:
<table align=centerborder=0 cellspacing=0 cellpadding=0>
<tr><tdalign="center"><iframe name=topFrmaesrc="proxy.servlet?objURL=${urlResource.topUrl}" frameborder=0width="920" scrolling=no></iframe></td></tr>
<tr><tdalign="center"><iframe name=homeFramesrc="<%=request.getContextPath()%>/home.do"frameborder=0 width="910" height="712"scrolling=no></iframe></td></tr>
<tr><tdalign="center"><iframe name=footFramesrc="proxy.servlet?objURL=${urlResource.buttomUrl}" frameborder=0width="920" scrolling=no></iframe></td></tr>
</table>