Reputation: 1353
I need the correct page height of a WebBrowser control.
In Javascript, this works:
var body = document.body;
var html = document.documentElement;
var height = Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight );
Duplicate questions exist on SO, with the the following methods suggested:
webBrowser.Document.Body.ScrollRectangle.Height;
webBrowser.Document.GetElementsByTagName("body")[0].OffsetRectangle.Bottom;
webBrowser.Document.GetElementsByTagName("body")[0].OffsetRectangle.Height;
Except none return the same value as the JS code.
I'm open to the possibility that the JS value is wrong but I am fairly confident it is correct because scrolling to the bottom of the page and adding the ScrollTop
position to the control height
produces the same value.
webBrowser1.Document.GetElementsByTagName("HTML")[0].ScrollTop + webBrowser1.Height;
For my particular case, C# is returning a height of 510, while JS returns correct height, 529. This inconsistency is throwing off a custom scroll bar I have written.
Upvotes: 2
Views: 1084
Reputation: 8194
If you have access to the code for the page then you can add the Javascript in your question to that page and then invoke it from the Webbrowser and retrieve the result.
HTML
<html>
<head>
<script>
function GetPageHeight()
{
var body = document.body;
var html = document.documentElement;
var height = Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight );
return height;
}
</script>
</head>
<body>
Test
</body>
</html>
C#
int height = (int)webBrowser1.Document.InvokeScript("GetPageHeight");
Upvotes: 3