程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> jsp頁面iframe高度自適應的js代碼,iframejs

jsp頁面iframe高度自適應的js代碼,iframejs

編輯:關於JSP

jsp頁面iframe高度自適應的js代碼,iframejs


以下操作寫在body裡面,form表單外

<iframe id="agentFrame" name="agentFrame" src="" width="0" height="0"  ></iframe> 
<script type="text/javascript"> 
function sethash(){ 
hashH = document.documentElement.scrollHeight; //獲取自身高度 
urlC = "/test.jsp"; //設置iframeA的src 
document.getElementById("agentFrame").src=urlC+"#"+hashH; //將高度作為參數傳遞 
} 
window.onload=sethash; 
</script>

test.jsp如下:

<%@ page contentType="text/html;charset=GBK" language="java"%> 
<script> 
function pseth() { 
var iObj = parent.parent.document.getElementById('infoFrame'); 
iObjH = parent.parent.frames["infoFrame"].frames["agentFrame"].location.hash; 
iObj.style.height = iObjH.split("#")[1]+"px"; 
} 
window.onload=pseth(); 
</script>

jsp iframe怎根據導入的頁面大小自動變化?

用一個JS來自動增加frame的高度,可以的<iframe id="myframe" name="myframe" width="100%" src="TheFirst.aspx" scrolling="no" frameborder="0" onload="this.height=100">
</iframe> <script type="text/javascript">
function reinitIframe()
{
var iframe = document.getElementById("myframe");
try{
var bHeight = iframe.contentWindow.document.body.scrollHeight;
var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
var height = Math.max(bHeight, dHeight);
iframe.height = height;
}catch (ex){}
}
window.setInterval("reinitIframe()", 200);
</script>
 

JS跨域控制iframe,讓iframe自適應高度的完整代碼

父頁面:stockPoolHistoryView.jsp
<head>
<script type="text/javascript">
function iframeStockPoolHistoryViewRecPoolGoGoGo(width,height) {
var appSubIframeObj=document.getElementById("stockPoolHistoryViewRecPool");
appSubIframeObj.style.height=height+"px";
}
</script>
</head>
<body>
<table>
<tr>
<td>
<iframe id="stockPoolHistoryViewRecPool"></iframe>
</td>
</tr>
</table>
</body>
子頁面:stockPoolHistoryViewRecRool.jsp
<head>
<script type="text/javascript">
function iframeHeightWidth(){
var height = document.body.scrollHeight;
var width = document.body.scrollWidth;
window.parent.iframeStockPoolHistoryViewRecPoolGoGoGo(width,height);
}
</script>
</head>
<body>
<fs:form>
</fs:form>
<script type="text/javascript">
iframeHeightWidth();
</script>
</body>
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved