blogspot.com-GA4

星期三, 12月 09, 2009

動態iframe使用

1. iframe使用的js檔案內容

var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
//extra height in px to add to iframe in FireFox 1.0+ browsers
var FFextraHeight=getFFVersion>=0.1? 16 : 0

function dyniframesize(iframename) {
var pTar = null;
if (document.getElementById){
pTar = document.getElementById(iframename);
}
else{
eval('pTar = ' + iframename + ';');
}
if (pTar && !window.opera){
//begin resizing iframe
pTar.style.display="block"

if (pTar.contentDocument && pTar.contentDocument.body.offsetHeight){
//ns6 syntax 在最後面加一點高度以避免頁面被切
pTar.height = pTar.contentDocument.body.offsetHeight+FFextraHeight + 20;
}
else if (pTar.Document && pTar.Document.body.scrollHeight){
//ie5+ syntax 在最後面加一點高度以避免頁面被切
pTar.height = pTar.Document.body.scrollHeight + 20;
}
}
}


2. 在iframe內屬性加入 id以及 onload function事件

// scrolling 設定成auto可以在載入非相同 DomanName 的頁面時,自動出現scrolling捲軸
<iframe name="main" id="myTestFrameID1" onload="javascript:dyniframesize('myTestFrameID1');" marginwidth="0" marginheight="0" frameborder="0" scrolling="auto" src="index1.jsp" width="800">main</iframe>

沒有留言: