4

I would like to run the following 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.

2 Answers 2

2

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.

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

4 Comments

thanks.. one more thing how can I call it, and would I put it like this: [webView stringByEvaluatingJavascriptFromString:"function foo() { document.body.contentEditable ='true'; document.designMode='on'; }"]; [webView stringByEvaluatingJavascriptFromString:foo];
You would have the function I wrote in your HTML file. That way, you can test that your code works in a browser, as opposed to an app, and, when you're sure it works, you could then have the iPhone call it. You would call it in iOS by doing: [webView stringByEvaluatingJavascriptFromString:@"foo()"]
oh I am not using an html file
Oh... so you want to call this on any HTML you load in the UIWebView, huh, not necessarily something you create yourself. My apologies. :)
0

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.

4 Comments

I tried that but it still does not work. Thanks for the quick answer
I edited my answer a couple of times. Did you perform all of the tasks above?
Yes I performed all of the tasks
hey, that doesn't work either. The page is currently still not editable.

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.