[html] 在iframe內的網頁取得外面這個iframe本身, 並設定高

keywords: window.frameElement

在iframe內的網頁如果要把外層這個iframe撐高的話
一般都是從內層去呼叫parent.document.getElementById('iframe的ID')
但其實還有更厲害的就是 window.frameElement

window.frameElement是說如果這個頁面有被另外的東西包住
(iframe, object)的話, 會返回這個東西, 也就是iframe

設定高的部份如下:

var doc = document,
    docHeight = Math.max(
      doc.body.scrollHeight,
      doc.documentElement.scrollHeight,
      doc.body.offsetHeight,
      doc.documentElement.offsetHeight,
      doc.body.clientHeight,
      doc.documentElement.clientHeight
    );
window.frameElement.setAttribute("height", docHeight);


如果是cross domain的話,以上方法就行不通了
必須使用window.postMessage來溝通

留言

這個網誌中的熱門文章

[翻譯] 介紹現代網路負載平衡與代理伺服器

Grafana K6

Linux 事件驅動筆記