views:

34

answers:

2

Hello fellows,

I'm wondering how to scroll programmatically to a given anchor in a WebView.

The content I am showing is rendered by

[[webView mainFrame] loadHTMLString:htmlString baseURL:someURL];

and thus I cannot simply navigate to #anchors by pointing them out in the URLs.

I'm looking for a method along the lines of

[[webView mainFrame] scrollToAnchor:@"anchor"]

but obviously it isn't there.

TIA

A: 

Ok, found a workaround, but I don't know if this is the right way to do it. By getting the reference to the javascript context I can call javascript methods in the webFrame.

[[webView windowScriptObject] evaluateWebScript:@"document.getElementById('TheId').scrollIntoView(true);"];
lambda64
+1  A: 

Using Javascript Bridge works, but you can also do the equivalent from Objective-C if you like:

DOMDocument *doc = [[webView mainFrame] DOMDocument];
DOMElement *element = [doc getElementById:@"anchor"];
[element scrollIntoView:YES];
Mike Abdullah
This certainly is the clean way I was looking for. Thanks!
lambda64