2017-07-15 146 views
0

我有一個要在其他網站上升級的iframe。我已經設置這是通過JavaScript檢查幀大小的最低金額:限制iframe大小

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

試試這個功能。

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); 
} 

祝你好運!