Search code examples
c++webviewscrollbarwxwidgets

Get scrollbar position wxWebView


The title says it all. Get the vertical scrollbar position from a wxWebView since wxWebView::GetScrollPos doesn't work (It always returns 0).


Solution

  • The idea is to run JavaScript code to get the scrollbar position into a variable named "output", since wxWidgets provides facilities to get the output of a chunk of JavaScript code (Unlike WebKitGTK).

    The code looks like this:

    wxString output;
    //Note that you can use the document.body.scrollLeft to get the horizontal scroll
    wxString get_document_text_script = "document.body.scrollTop";
    if(!webview->RunScript(get_document_text_script, &output))
    {
        output = "0"; //Safeguard against errors or half-loaded pages
    }