Help Using Javascript Code in a UIWebView
I would like to run the f开发者_JS百科ollowing code in a webView, so that the content is editable:
javascript: document.body.contentEditable ='true'; document.designMode='on'; void 0
So far I have got as far as trying this:
- (void)webViewDidFinishLoad:(UIWebView *)webViews
{
NSString *string = @"javascript: document.body.contentEditable ='true'; document.designMode='on'; void 0";
[webView stringByEvaluatingJavaScriptFromString:string];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:string]]];
NSLog(@"finished load");
}
If you could tell me how to get this working that would be great.
I like wrapping up my JavaScript calls in JavaScript. What I mean by this is, if there are multiple lines to be run in JavaScript, to wrap that in a function and call just the function. This separates the logic out cleanly and let's you test things strictly in Safari without building an iPhone app around it.
Also, I agree with what Evan said about getting rid of the [webView loadRequest:
.
EDIT: By wrapping up, I mean something like:
function foo() { document.body.contentEditable ='true'; document.designMode='on'; }
And then just calling "foo" in your call from iOS.
Remove the JavaScript: prefix. That method already knows that you will be evaluating JavaScript, so there is no need for redundancy.
Also, don't send that request at the end. Evaluating using that method will execute it on the current page.
精彩评论