1
[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

2
  • 1
    Did you ensure that your external js file was parsed? The method must be known to WebView at time of calling. Commented Mar 1, 2013 at 8:12
  • the method is globally defined in js file and the js file is referred in the html file as <script type="text/javascript" src="webview-script.js"></script> Commented Mar 1, 2013 at 9:44

1 Answer 1

1

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.