开发者

UIWebView not evaluating javascript properly

开发者 https://www.devze.com 2023-01-03 14:06 出处:网络
I\'m trying to run some javascript against a UIWebView but it\'s doesn\'t seem to be working. For example, here i开发者_如何学编程s a snippet of html I\'m using to test:

I'm trying to run some javascript against a UIWebView but it's doesn't seem to be working. For example, here i开发者_如何学编程s a snippet of html I'm using to test:

<html><body><h1>Hello World!</h1><p>It's me.</p></body></html>

If I run the following javascript against it, the return value is 0, when it should be 1.

var elements = document.getElementsByTagName("h1");
elements.length;

Here's the objc code I'm using for the webview. 'parser' is a string containing the above javascript:

[webView loadHTMLString:@"<html><body><h1>Hello World!</h1><p>It's me.</p></body></html>" baseURL:nil];
NSString *markupResult = [webView stringByEvaluatingJavaScriptFromString:parser];


Implement the UIWebViewDidfinishLoading method and write following code inside it

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *markupResult = [webView stringByEvaluatingJavaScriptFromString:parser];
}


Upon further investigation, the problem turned out to be that webview hadn't finished loading when the javascript was called. I had to call the javascript from the webViewDidFinishLoad delegate callback.

0

精彩评论

暂无评论...
验证码 换一张
取 消