try catch finally
try catch finally

Reputation: 881

Search text using #hashtag and scroll in UIWebView

I have loaded text in a UIWebView. Each sentence is under the anchor <a> tag. The <a> tag's href attribute has the sentence number like <a href = '#%d'>. Now when the user search, the UIWebView should scroll up to the line or the <a> tag.

I used some js code but it doesn't work.

1.

NSString *jscript = [NSString stringWithFormat:@"window.location.href = '%d';", self.selectedIndex];
[webView stringByEvaluatingJavaScriptFromString:jscript];

2.

NSString *jscript = [NSString stringWithFormat:@"window.location=\"myapp#%d\";", self.selectedIndex];
[webView stringByEvaluatingJavaScriptFromString:jscript];

3.I was trying to use this but couldn't implement

NSString *jscript = [NSString stringWithFormat:@"$(document).ready(function() { %d;}", self.selectedIndex];
[webView stringByEvaluatingJavaScriptFromString:jscript];

I have negligible knowledge of javascript, may be that is why I am not able to understand where I am going wrong.

Any help would be highly appreciated.

Upvotes: 1

Views: 469

Answers (1)

souvickcse
souvickcse

Reputation: 7804

You can scroll to position by simply writing few lines of javascript . If you want to scroll to that position at the page load you can use

<script> 
var el = document.getElementById("id_of_the_element"); 
window.onload = function () { 
window.scrollTo(20,el.offsetTop); 
} </script>

Upvotes: 1

Related Questions