Question

I'm trying to navigate within a webpage that has been loaded from a remote server in my WebView control (Cocoa application). I would like to navigate to a particular tag that i can see in the HTML code of that page. The purpose of this all is to show the part of the HTML page that is of my interest at the top of the WebView control.

I know that in HTML code you can navigate by using something like #MIDDLE, #TOP etc. However, is this possible to do from outside of the HTML code using the WebView API?

Thanks for your reply in advance!

Was it helpful?

Solution

I found the answer to my question with the help of an other question (How to scroll HTML page to given anchor using jQuery or Javascript?).

The piece of code below does the trick for me. It searches for HTML elements with attribute: class = "container" in the HTML data that is loaded in the WebView component self.webView.

-(void) scrollMyImportantHTMLPartInView
{
    // Get a list of HTML elements that contain attribute class = "container" (eg. <div class "container">)
    DOMNodeList *nodeList = [[[self.webView mainFrame] DOMDocument] getElementsByClassName: @"container"];
    if( nodeList && nodeList.length >= 1 ) {

        // get the first node (class = "container") from the list
        DOMNode *domNode = [nodeList item:0];

        // Make sure it's a DOM element type
        if( domNode.nodeType == DOM_ELEMENT_NODE ) {

            // It's now save to cast from DOMNode* to DOMElement*
            DOMElement* domElement = (DOMElement*) domNode;

            // Scroll begining of HTML node into view
            [domElement scrollIntoView: YES];
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top