js實(shí)現(xiàn)的真正的iframe高度自適應(yīng)(兼容IE,FF,Opera)
更新時(shí)間:2010年03月07日 12:15:07 作者:
由于項(xiàng)目上的需要,要用一個(gè)iframe高度自適應(yīng)的功能,在google上搜了很久,找了一些修改了下。大家可以測試下。
找到了下面這個(gè)js
function SetCwinHeight(obj)
{
var cwin=obj;
if (document.getElementById)
{
if (cwin && !window.opera)
{
if (cwin.contentDocument && cwin.contentDocument.body.offsetHeight)
cwin.height = cwin.contentDocument.body.offsetHeight + 20;
else if(cwin.Document && cwin.Document.body.scrollHeight)
cwin.height = cwin.Document.body.scrollHeight + 10;
}
}
}
然后……
進(jìn)入了測試過程(調(diào)用很簡單,先略過)
1.IE ---通過 但是高度還是有稍微的差距,很小,滾動條還在
2.FF --- 通過 與IE一樣,有小差距
3.Opera --- 看那個(gè)JS的條件就知道,通不過的
但主流瀏覽器至少要通過這三項(xiàng)撒?。?!
于是,還是Google
搜索 各瀏覽器在處理 document.scrollHeight 或者 offsetHeigth時(shí)的特殊現(xiàn)象
發(fā)現(xiàn),Opera瀏覽器在處理iframe內(nèi)容的時(shí)候,用的是contentWindow
而處理內(nèi)容高度的時(shí)候,卻與IE一致
從而,有了下面這段js
<html>
<head>
<script>
function SetCwinHeight(obj)
{
var cwin=obj;
if (document.getElementById)
{
if (cwin && !window.opera)
{
if (cwin.contentDocument && cwin.contentDocument.body.offsetHeight)
cwin.height = cwin.contentDocument.body.offsetHeight + 20; //FF NS
else if(cwin.Document && cwin.Document.body.scrollHeight)
cwin.height = cwin.Document.body.scrollHeight + 10;//IE
}
else
{
if(cwin.contentWindow.document && cwin.contentWindow.document.body.scrollHeight)
cwin.height = cwin.contentWindow.document.body.scrollHeight;//Opera
}
}
}
</script>
</head>
<body>
<iframe src="20103622440.html" onload="SetCwinHeight(this);" width="600px">
</body>
</html>
這樣一來,總算把這三個(gè)瀏覽器給適應(yīng)了
做為程序員,還是要細(xì)心點(diǎn)
再測試一下
OK...3個(gè)瀏覽器均正常顯示,也無iframe的縱向滾動條了
復(fù)制代碼 代碼如下:
function SetCwinHeight(obj)
{
var cwin=obj;
if (document.getElementById)
{
if (cwin && !window.opera)
{
if (cwin.contentDocument && cwin.contentDocument.body.offsetHeight)
cwin.height = cwin.contentDocument.body.offsetHeight + 20;
else if(cwin.Document && cwin.Document.body.scrollHeight)
cwin.height = cwin.Document.body.scrollHeight + 10;
}
}
}
然后……
進(jìn)入了測試過程(調(diào)用很簡單,先略過)
1.IE ---通過 但是高度還是有稍微的差距,很小,滾動條還在
2.FF --- 通過 與IE一樣,有小差距
3.Opera --- 看那個(gè)JS的條件就知道,通不過的
但主流瀏覽器至少要通過這三項(xiàng)撒?。?!
于是,還是Google
搜索 各瀏覽器在處理 document.scrollHeight 或者 offsetHeigth時(shí)的特殊現(xiàn)象
發(fā)現(xiàn),Opera瀏覽器在處理iframe內(nèi)容的時(shí)候,用的是contentWindow
而處理內(nèi)容高度的時(shí)候,卻與IE一致
從而,有了下面這段js
復(fù)制代碼 代碼如下:
<html>
<head>
<script>
function SetCwinHeight(obj)
{
var cwin=obj;
if (document.getElementById)
{
if (cwin && !window.opera)
{
if (cwin.contentDocument && cwin.contentDocument.body.offsetHeight)
cwin.height = cwin.contentDocument.body.offsetHeight + 20; //FF NS
else if(cwin.Document && cwin.Document.body.scrollHeight)
cwin.height = cwin.Document.body.scrollHeight + 10;//IE
}
else
{
if(cwin.contentWindow.document && cwin.contentWindow.document.body.scrollHeight)
cwin.height = cwin.contentWindow.document.body.scrollHeight;//Opera
}
}
}
</script>
</head>
<body>
<iframe src="20103622440.html" onload="SetCwinHeight(this);" width="600px">
</body>
</html>
這樣一來,總算把這三個(gè)瀏覽器給適應(yīng)了
做為程序員,還是要細(xì)心點(diǎn)
再測試一下
OK...3個(gè)瀏覽器均正常顯示,也無iframe的縱向滾動條了
您可能感興趣的文章:
- 關(guān)于div自適應(yīng)高度/左右高度自適應(yīng)一致的js代碼
- JS實(shí)現(xiàn)iframe自適應(yīng)高度的方法示例
- JS實(shí)現(xiàn)很實(shí)用的對聯(lián)廣告代碼(可自適應(yīng)高度)
- JS實(shí)現(xiàn)自適應(yīng)高度表單文本框的方法
- js實(shí)現(xiàn)iframe自動自適應(yīng)高度的方法
- 使用javascript實(shí)現(xiàn)Iframe自適應(yīng)高度
- js控制iframe的高度/寬度讓其自適應(yīng)內(nèi)容
- 兼容主流瀏覽器的iframe自適應(yīng)高度js腳本
- JavaScript 處理Iframe自適應(yīng)高度(同或不同域名下)
- Javascript 文本框textarea高度隨內(nèi)容自適應(yīng)增長收縮
- JS實(shí)現(xiàn)DIV高度自適應(yīng)窗口示例
相關(guān)文章
setInterval計(jì)時(shí)器不準(zhǔn)的問題解決方法
在js中如果打算使用setInterval進(jìn)行倒數(shù),計(jì)時(shí)等功能,往往是不準(zhǔn)確的,針對這個(gè)問題,本文有個(gè)不錯(cuò)的解決方案2014-05-05
js從Cookies里面取值的簡單實(shí)現(xiàn)
遇到一個(gè)Js從Cookies里面取值的需求,Js貌似沒有現(xiàn)成的方法可以指定Key值獲取Cookie里面對應(yīng)的值,簡單實(shí)現(xiàn)如下2014-06-06
bootstrap中添加額外的圖標(biāo)實(shí)例代碼
可以針對校驗(yàn)狀態(tài)為輸入框添加額外的圖標(biāo)。接下來通過本文給大家分享bootstrap中添加額外的圖標(biāo)實(shí)例代碼,需要的的朋友參考下吧2017-02-02

