Calling Javascript using UIWebView(使用 UIWebView 调用 Javascript)
问题描述
我正在尝试使用该函数在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 UIWebView 调用 Javascript
基础教程推荐
- 如何添加到目前为止的天数? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
