Skip to content

Jump To A HTML Anchor in a WebView

Categories: Cocoa

Table of Contents

This isn’t something that is as straightfoward as you would think. There is no real ‘cocoa native’ way to acomplish this, and there seemed to be no information anywhere on how to acomplish this until I found this post. So it seems the only way to scroll to an anchor is to resort to evaluating javascript code:
[code lang=”javascript”]
– (void) jumpToAnchor:(NSString *)anchor {
[oWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@”var anchor = document.anchors[“%@”];window.scrollTo(anchor.offsetLeft, anchor.offsetTop);”, anchor]];
}
[/code]