限制iframe大小

时间:2017-07-15 06:43:20

标签: javascript iframe

我有一个iframe,将在其他网站上推广。我已经设置了通过javascript检查的最小帧大小:

function check() {
    var w0 = screen.width;
    var h0 = screen.height;
    var w = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
    var h = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;

    if ((w0 < 700) || (w < 650) || (h0 < 500) || (h < 400)) {
    window.location = "stop.html";

根据我们的测试,这是有效的。但是,我发现用户通过将iframe嵌入width / height = 0来绕过它。该脚本无法捕获它,因为它似乎检查实际页面的嵌入大小。

那么如何检查框架的大小呢?

1 个答案:

答案 0 :(得分:1)

尝试此功能。

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 );
}
祝你好运!