问题描述
我正在尝试使用该函数在 html 页面中调用 javascript -
I am trying to call a javascript in a html page using the function -
View did load function
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"BasicGraph.html"];
NSURL *urlStr = [NSURL fileURLWithPath:writablePath];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *myPathInfo = [[NSBundle mainBundle] pathForResource:@"BasicGraph" ofType:@"html"];
[fileManager copyItemAtPath:myPathInfo toPath:writablePath error:NULL];
[graphView loadRequest:[NSURLRequest requestWithURL:urlStr]];
}
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
[graphView stringByEvaluatingJavaScriptFromString:@"methodName()"];
}
这是 html 页面上的 javascript -
Here is the javascript on the html page -
<script>
function methodName()
{
// code to draw graph
}
但是,函数 methodName() 没有被调用,但是在 window.onload = function () 之后一切正常..
However, the function methodName() is not getting called but after window.onload = function () everything is working fine..
我正在尝试将 RGraphs 集成到我的应用程序中,而 Basic.html 是其中的 html 页面javascript是写的.
I am trying to integrate RGraphs into my application and Basic.html is the html page in which the javascripts are written.
如果有人能帮我解决这个问题,那就太好了.
It would be great if someone could help me out with this.
推荐答案
简单:你尝试在页面加载之前从 Objective-C 执行 JS 函数.
Simple: You try to execute the JS function from Objective-C before the page even has been loaded.
实现 UIWebView 的委托方法 webViewDidFinishLoad: 在您的 UIViewController 中并在其中调用 [graphView stringByEvaluatingJavaScriptFromString:@"methodName()"]; 以确保在页面之后调用该函数已加载.
Implement the UIWebView's delegate method webViewDidFinishLoad: in your UIViewController and in there you call [graphView stringByEvaluatingJavaScriptFromString:@"methodName()"]; to make sure the function gets called after the page has been loaded.
这篇关于使用 UIWebView 调用 Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)