开发者

WPF WebBrowser Content Size

I reviewed all of the related questions but was unable to get an answer.

How do I get the current content size of the WebBrowser 开发者_Python百科control in WPF?


To determine the actual size(using javascript) of the browser window, use the following properties:

in Internet Explorer (backward-compatibility mode):

document.body.offsetWidth, document.body.offsetHeight

in Internet Explorer (standards mode, document.compatMode=='CSS1Compat'):

document.documentElement.offsetWidth, document.documentElement.offsetHeight

in most other browsers:

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

In your browser, this code produces the following output:

Window width = 1280
Window height = 675

Notes:

  1. If the above code executes within a frame or iframe, it will give you the frame's width and height.
  2. The computed width and height do not include the title bar, menu bar, status bar, or toolbar(s) - but may include the horizontal and vertical scrollbar(s), if any.
  3. The code that uses document.body.offsetWidth and offsetHeight should be executed after the browser has parsed the tag.
  4. If the user resizes the browser window, you may need to recompute the width and height (use window.onresize to do so).
  5. Similarly, if the user zooms in (or zooms out) you may also need to recompute the width and height.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜