簡體   English   中英

檢測 <iframe> iframe內部的高度和寬度

[英]Detect <iframe> height and width from inside the iframe

我懷疑這實際上是可能的,但我准備糾正。

我需要的是能夠從iframe中檢測iframe的寬度和高度,所以如果iframe srciframeContent.html我需要能夠在這個頁面上顯示值。

我無法控制僅托管iframe的頁面iframe的內容。

這可能嗎?

您始終可以獲取網頁尺寸,無論頁面是在iframe加載還是在新窗口中加載,它始終以相同的方式工作。 這是我發現用於獲取window尺寸的函數,您也可以使用它來獲取<iframe>尺寸。

function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM