4

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\";"];   
}                                                     

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.