infobug
infobug

Reputation: 313

Jump through search results in UIWebView with Javascript

I have set up a search in uiwebview with javascript that works great, but I want to be able to jump to the next found word in the search results. I have succeeded in geting the view to scroll to the first instance by using this code:

if (uiWebview_SearchResultCount == 1)
{
   var desiredHeight = span.offsetTop - 140;
   window.scrollTo(0,desiredHeight);
}

How can I get this searchresultcount to update to the next found result(say 2, 3, 4, 5, ect...) when user presses button in app?? Thanks in advance.

Upvotes: 1

Views: 1211

Answers (3)

Circle B
Circle B

Reputation: 1674

I was able to do basically the same thing with this javascript in the webview:

<script type="text/javascript">
var max = 100;

function goToNext() {
    var hash = String(document.location.hash);
    if (hash && hash.indexOf(/hl/)) {
        var newh = Number(hash.replace("#hl",""));
        (newh > max-1) ? newh = 0 : void(null);
        document.location.hash = "#hl" + String(newh-1); 
    } else {
        document.location.hash = "hl1";        
    }
}

</script>

Then sending this JavaScript call with my IBAction like this:

- (IBAction)next:(id)sender {
    [animalDesciption stringByEvaluatingJavaScriptFromString:@"goToNext()"];
}

Upvotes: 0

Wiz
Wiz

Reputation: 474

call this function with the appropriate row?

function scrollToDesiredHeight(row) {
   var desiredHeight = span.offsetTop - 140;
   window.scrollTo(0,row * desiredHeight);
}

Does this work for you?

Upvotes: 0

Jake Stoeffler
Jake Stoeffler

Reputation: 2885

Do you mean a native button in your app such as a UIButton? In that case, you can use stringByEvaluatingJavaScriptFromString: to execute some JavaScript in your UIWebView. You could do something like this as the handler for your button:

- (void)buttonPressedAction:(id)sender {
    NSString * js = @"uiWebview_SearchResultCount++;";
    [yourUIWebView stringByEvaluatingJavaScriptFromString: js];
}

Upvotes: 1

Related Questions