UIWebView stringByEvaluatingJavaScriptFromString 没有调用 javascript 函数 [英] UIWebView stringByEvaluatingJavaScriptFromString isn't calling javascript function

查看:23
本文介绍了UIWebView stringByEvaluatingJavaScriptFromString 没有调用 javascript 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制器中有以下内容,但不明白为什么它不调用页面中的函数(它不执行任何操作).任何帮助表示赞赏 - 第一次使用这个:

I have the following in a controller and don't understand why it is not calling the function in the page (it doesn't do anytihng). Any help is appreciated - first time using this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *fullURL = @"http://local.com/js-more.html";  // this does laod
    NSURL *url = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [arcWebView loadRequest:requestObj];

    NSString * param  = @"foo";   
    NSString * jsCallBack = [NSString stringWithFormat:@"myFunc('%@')",param];
    [arcWebView stringByEvaluatingJavaScriptFromString:jsCallBack];

在我的网页中:

<script>

function myFunc(this_str){![enter image description here][1]
  alert('here is val: ' + this_str);
}
myFunc('here is in string');
</script>

这会在浏览器或嵌入式 UIWebView 中正确发出警报.

and this gets alerted correctly in a browser or in an embedded UIWebView.

提前谢谢

编辑 1

推荐答案

我将 javascript 放在 webviewdidfinishload 委托方法中.由于问题可能是您的网页尚未加载,即使如此您也调用了加载请求.

I'd put the javascript inside the webviewdidfinishload delegate method. As the problem may be that your webpage is not loaded yet, even so you have called load request.

设置 webviews 委托,将代码放在那里,它应该可以工作.

set the webviews delegate, put the code there, and it should work.

EG

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *fullURL = @"http://local.com/js-more.html";  // this does laod
    NSURL *url = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [arcWebView setDelegate:self];
    [arcWebView loadRequest:requestObj];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSString * param  = @"foo";   
    NSString * jsCallBack = [NSString stringWithFormat:@"myFunc('%@')",param];
    [webView stringByEvaluatingJavaScriptFromString:jsCallBack];
}

这篇关于UIWebView stringByEvaluatingJavaScriptFromString 没有调用 javascript 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆