stringByEvaluatingJavaScriptFromString不起作用 [英] stringByEvaluatingJavaScriptFromString does not work
本文介绍了stringByEvaluatingJavaScriptFromString不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从 UIWebView
中加载的本地html文件调用javascript函数,但它没有响应
I am trying to call a javascript function from loaded local html file in UIWebView
but it deosn't respond
它看起来像这样
NSString *script=[NSString stringWithFormat:@"sample()"];
if (tekst.loading) {
NSLog(@"Loading");
} else {
NSLog(@"Fully loaded");
[tekst stringByEvaluatingJavaScriptFromString:script];
}
和html
<head>
....
<script type='text/javascript'>
function sample() {
alert('Paznja');
}
</script>
...
</head>
推荐答案
它看起来好像你没有使用委托你的UIWebView。如果您设置一个委托并将对Javascript的调用放入 - (void)webViewDidFinishLoad:(UIWebView *)webView方法,它可以正常工作:
It looks to me as if you are not using a delegate on your UIWebView. If you set a delegate and put the call to the Javascript into the "- (void)webViewDidFinishLoad:(UIWebView *)webView " method, it works fine:
UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 0.0, 700.0, 700.0)];
[self.view addSubview:wv];
wv.delegate = self;
[wv loadHTMLString:@"<html><head><script type='text/javascript'>function sample() {alert('Paznja');}</script></head><body><h1>TEST</h1></body></html>" baseURL:nil];
[wv release];
和同一类:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[webView stringByEvaluatingJavaScriptFromString:@"sample()"];
}
当我运行此代码时,警报消息正常。
When I run this code, the alert message works fine.
这篇关于stringByEvaluatingJavaScriptFromString不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文