I can set the scroll-position with WebBro开发者_Go百科swer.Document.Window.ScrollTo()
, but how do I read the current scroll-position? Also, how do I read the total height of the document?
Background: I want to make a WebBrowser
displaying logs to automatically scroll down as new messages come in, but only if it's already scrolled all the way to the bottom, so that the user can scroll up inspect older messages without interruption.
i can't give you the WinForms version directly, but i can give you the version we used from managed code.
But what i wanted to really say was that you have to be careful to know if your document is in
- Standards Mode: i.e. Internet Explorer 6 or newer
- Quirks Mode: i.e. Internet Explorer 5.5 compatible mode
The reason you want to know this is because the attribute you want to query depends if the browser is in standards or quirks mode. A document in quirks mode does not support documentElement.scrollHight
(i.e. it returns zero).
returns the height of...
Expression Quirks Mode Standards Mode
===================================== ========================== ==========================
document.body.scrollHeight document body
document.documentElement.scrollHeight (not supported) document
You want the document height, so the way to return that is:
- Standards Mode:
document.documentElement.scrollHeight
(returns height of document) - Quirks mode:
document.body.scrollHeight
(actually returns height of document, not body)
So i actually have two helper functions:
int WebBrowserScrollHeightQuirksMode(WebBrowser wb);
int WebBrowserScrollHeightStandardsMode(WebBrowser wb);
Which forces you to figure out if your html is in Netscape 2.0 compatible mode, and figure out which one you want to call:
function WebBrowserScrollHeightQuirksMode(const WebBrowser: TWebBrowser): Integer;
var
doc: IHTMLDocument2;
begin
{
Expression Mode Returns ScrollHeight of
document.body.scrollHeight Quirks document
document.body.scrollHeight Standards body
document.documentElement.scrollHeight Standards document
We *want* the scroll height of the document, which means we should have been
using document.documentElement.scrollHeight
WARNING: A document in quirks mode does not support documentElement[scrollHight]
i.e. it returns zero
}
doc := (WebBrowser.Document as IHTMLDocument2);
if not Assigned(doc) then
Result := 0
else if not Assigned(doc.body) then
Result := 0
else
Result := doc.body.getAttribute('scrollHeight', 0);
end;
And the version when the HTML is in standards mode (i.e. Internet Explorer 6 or newer):
function WebBrowserScrollHeightStandardsmode(const WebBrowser: TWebBrowser): Integer;
var
doc: IHTMLDocument2; //ie4, has doc.body
doc3: IHTMLDocument3; //ie5, has doc.documentElement
begin
//Use this for IE6 or newer, with the browser in standards mode
Result := 0;
{
Expression Mode Returns ScrollHeight of
===================================== ========= =======================
document.body.scrollHeight Quirks document
document.body.scrollHeight Standards body
document.documentElement.scrollHeight Standards document
We *want* the scroll height of the document, which means we should have been
using document.documentElement.scrollHeight
WARNING: A document in quirks mode does not support documentElement[scrollHight]
i.e. it returns zero
}
doc := (WebBrowser.Document as IHTMLDocument2);
if not Assigned(doc) then
Exit;
//Should use doc3.documentElement scrollHeight
if Supports(doc, IHTMLDocument3, doc3) then //Requires Windows 95, IE5
begin
if Assigned(doc3.documentElement) then
begin
Result := VarAsInteger(doc3.documentElement.getAttribute('scrollHeight', 0));
Exit;
end;
end;
end;
精彩评论