Reputation: 954
I have the following code in a view controller. "pageDisplay" is a UIWebView.
When I run the app in simulator, the HTML page comes up as it appears before the JS runs. The element with id "myHeader", an <h1>
tag, is unchanged.
-(void) loadPageToView:(int)pageNumber{
NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d",pageNumber]ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[pageDisplay loadRequest:request];
[self doJavaScript];
}
-(void) doJavaScript{
[pageDisplay stringByEvaluatingJavaScriptFromString:@"document.getElementById('myHeader').innerHTML = \"FOO\";"];
}
Upvotes: 4
Views: 2015
Reputation: 11920
You're calling doJavaScript
too early. You need to wait until the page is loaded. Investigate the web view delegate method - (void)webViewDidFinishLoad:(UIWebView *)webView
.
Upvotes: 6