Lochana Ragupathy
Lochana Ragupathy

Reputation: 4320

calling javascript method from UIWebView

[self.webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"myfunc('%@')",string]];

This above code works very well and absolutely no problem but only when my javascript method is present in the page which i am loading. But if the js method is referred in any external js file it is not being triggered and this is how i am loading my page

       [self.webview loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://example/mypage.html"]]];

Thanks in advance

Upvotes: 1

Views: 261

Answers (1)

user660808
user660808

Reputation: 224

To be sure that your javascript function will be called only after the page is completely loaded, you have to call it inside the webViewDidFinishLoad: implementation.

So, for example:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"myfunc('%@')",string]];
    // Do any additional stuff
}

Upvotes: 1

Related Questions