JavaScript 處理Iframe自適應高度的問題

來源:互聯網
上載者:User

1.同網域名稱下Iframe自適應高度的處理

<iframe  onload="Javascript:SetIFrameHeight(this)" src="../Home/b" id="win" name="win" width="100%" height="1"> </iframe>

 當然此處我用的是Asp.Net MVC  此處src設定為路由結構

<script type="text/javascript">    function SetIFrameHeight(obj) {        var win = obj;        if (document.getElementById) {            if (win && !window.opera) {                if (win.contentDocument && win.contentDocument.body.offsetHeight)                    win.height = win.contentDocument.body.offsetHeight;                else if (win.Document && win.Document.body.scrollHeight)                    win.height = win.Document.body.scrollHeight;            }        }    }</script>

 用到的就是iframe嵌套的頁面載入完畢的時候,運用onload事件來擷取嵌套在iframe中網頁的高度,然後賦值給Iframe的高度即可。

2.跨域時Iframe高度自適應

 

在首頁面和被嵌套的iframe為不同網域名稱的時候,就稍微麻煩一些,需要避開JavaScript的跨域限制。
原理:現有iframe首頁面main.html、被iframe嵌套頁面iframe.html、iframe中介頁面agent.html三個,通過main.html(網域名稱為http://www.ccvita.com)嵌套iframe.html(網域名稱為:http://www.phpq.net),當使用者瀏覽時執行iframe.html中的JavaScript代碼設定iframeC的scr地址中加入iframe頁面的高度,agent.html(網域名稱為:http://www.ccvita.com)取得傳遞的高度,通過JavaScript設定main.html中iframe的高度。最終實現預期的目標。
iframe首頁面main.html
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>iframe首頁面</title></head>
<body>

<div style="border:1px solid #ccc;padding:10px;">
<iframe id="frame_content"  name="frame_content" src="iframe.html" width="100%" height="0" scrolling="no" frameborder="0"></iframe>
</div>
<br />尾部<br />

</body>
</html>
iframe嵌套頁面iframe.html
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>被iframe嵌套頁面</title></head>
<body>

文字<br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
文字<br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
文字<br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
文字<br /><br /><br /><br /><br /><br /><br /><br /><br /><br />

<iframe id="iframeC" name="iframeC" src="" width="0" height="0" style="display:none;" ></iframe>

<script type="text/javascript">
function sethash(){
    hashH = document.documentElement.scrollHeight;
    urlC = "agent.html";
    document.getElementByIdx("iframeC").src=urlC+"#"+hashH;
}
window.onload=sethash;
</script>

</body>
</html>
iframe中介頁面agent.html
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>iframe中介頁面</title></head>

<body>

<script>
function  pseth() {
    var iObj = parent.parent.document.getElementByIdx('frame_content');
    iObjH = parent.parent.frames["frame_content"].frames["iframeC"].location.hash;
    iObj.style.height = iObjH.split("#")[1]+"px";
}
pseth();
</script>

</body>
</html>
代碼裡,kimi可能路徑表示不全
main.html 與 agent.html 是必須同一個域裡
而iframe.html在另一個域

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.