加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

stringByEvaluatingJavaScriptFromString不起作用

发布时间:2020-12-14 19:55:12 所属栏目:百科 来源:网络整理
导读:我试图从UIWebView中加载的本地html文件中调用 javascript函数 但它没有回应 它看起来像这样 NSString *script=[NSString stringWithFormat:@"sample()"];if (tekst.loading) { NSLog(@"Loading");} else { NSLog(@"Fully loaded"); [tekst stringByEvaluati
我试图从UIWebView中加载的本地html文件中调用 javascript函数
但它没有回应

它看起来像这样

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”方法,它可以正常工作:

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()"];
}

当我运行此代码时,警报消息正常工作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读