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
基础教程推荐
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01