views:

103

answers:

1

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\";"];   
}                                                     
+2  A: 

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.

Benedict Cohen
Hooray! Thanks.
Chris