加载html是让stringByEvaluatingJavaScriptFromString正常工作的正确方法
我正在尝试让stringByEvaluatingJavaScriptFromString工作。 我肯定我加载的html文件是错误的,我只是不知道我做错了什么加载html是让stringByEvaluatingJavaScriptFromString正常工作的正确方法,javascript,html,ios,webview,uiwebview,Javascript,Html,Ios,Webview,Uiwebview,我正在尝试让stringByEvaluatingJavaScriptFromString工作。 我肯定我加载的html文件是错误的,我只是不知道我做错了什么 -(void)viewDidLoad { // Load the index.html file as a string from the file system NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
-(void)viewDidLoad
{
// Load the index.html file as a string from the file system
NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
// Load/pass it in to webView
[webView loadHTMLString:html baseURL:[[NSBundle mainBundle] bundleURL]];
[super viewDidLoad];
}
我将javascript事件传递/发送到index.html:
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[webView stringByEvaluatingJavaScriptFromString:@"displayName('name');"];
}
我做错了什么?这是我的函数代码
NSString *fullURL = @"url";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
完成负荷
-(void)webViewDidFinishLoad:(UIWebView *)webViews {
NSString *testString = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"[ID]\").innerHTML;"];
[webView loadHTMLString:testString baseURL:nil];
}
对代码进行注释。您在哪里声明testString?该文件位于应用程序的资源包中,未联机。我编辑了代码,但尚未尝试脱机使用该代码。我找到了该代码不起作用的原因。。。显示名称'name';他很早就被派去了。webView加载完成时。content index.html未完成加载,因此使事件提前。