1
A
回答
4
爲了確定實際尺寸(使用javascript)瀏覽器窗口,使用以下屬性:
在Internet Explorer(向後兼容模式):
document.body.offsetWidth, document.body.offsetHeight
在Internet Explorer
(標準模式,document.compatMode == 'CSS1Compat'):
document.documentElement.offsetWidth, document.documentElement.offsetHeight
在大多數其他瀏覽器
:
window.innerWidth, window.innerHeight
The following code sets the variables winW and winH to the actual width and height of the browser window, and outputs the width and height values. If the user has a very old browser, then winW and winH are set to 630 and 460, respectively.
var winW = 630, winH = 460;
if (document.body && document.body.offsetWidth) {
winW = document.body.offsetWidth;
winH = document.body.offsetHeight;
}
if (document.compatMode=='CSS1Compat' &&
document.documentElement &&
document.documentElement.offsetWidth) {
winW = document.documentElement.offsetWidth;
winH = document.documentElement.offsetHeight;
}
if (window.innerWidth && window.innerHeight) {
winW = window.innerWidth;
winH = window.innerHeight;
}
document.writeln('Window width = '+winW);
document.writeln('Window height = '+winH);
在瀏覽器中,此代碼產生以下輸出:
Window width = 1280
Window height = 675
注:
- 如果上面的代碼框架或iframe中執行,它會給你的幀的寬度和高度。
- 計算出的寬度和高度不包括標題欄,菜單欄,狀態欄或工具欄 - 但可能包含水平和垂直滾動條(如果有)。
- 應該在瀏覽器解析標籤後執行使用document.body.offsetWidth和offsetHeight的代碼。
- 如果用戶調整瀏覽器窗口大小,則可能需要重新計算寬度和高度(使用window.onresize來實現)。
- 同樣,如果用戶放大(或縮小),您可能還需要重新計算寬度和高度。
相關問題
- 1. 打印WPF WebBrowser的內容
- 2. WPF大小RichTextBox根據內容
- 3. WPF框架 - 大小到內容
- 4. WPF WebBrowser - 如何縮放內容?
- 5. WPF WebBrowser - 未打開插件內容
- 6. 在WebBrowser控件中檢測HTML內容的實際大小
- 7. Winforms WebBrowser設置最大內容寬度
- 8. iframe內容大小
- 9. UIWebView內容大小
- 10. Chromium WebBrowser的內容模糊
- 11. 小容器大小的WPF控件
- 12. EditText內容大小vs佈局大小
- 13. WebBrowser無法正確顯示內容
- 14. WebBrowser控件調整大小
- 15. iPhone - UITableViewController的內容大小
- 16. Android Textview的內容大小
- 17. WPF:如何在Popup中自動調整WebBrowser的大小?
- 18. 等待WebBrowser ajax內容
- 19. 打印WebBrowser控件內容
- 20. WPF防止動態網格內容超出父級大小
- 21. WPF窗體應調整大小以顯示其內容
- 22. WPF:使用GridSplitter觸發內容大小調整
- 23. 在WPF中自動調整窗口內容大小
- 24. 阻止使用WPF WebBrowser顯示內容的滾動條
- 25. 更改WPF WebBrowser控件上的「活動內容」安全設置
- 26. 將div縮小爲內容大小
- 27. iAds iOS 4.2中傳遞給ADAdSizeForBannerContentSize的內容大小「無效內容大小」
- 28. 內容容器沒有調整大小以適合內容
- 29. WPF WebBrowser NavigateToStream&images
- 30. WPF WebBrowser問題