简体   繁体   中英

UIWebView stringByEvaluatingJavaScriptFromString does not work second time

Within UITableViewController I have a UIWebView with stringByEvaluatingJavaScriptFromString that is properly loaded the first time. Then the View Controller is dimissed upon user's 'Close', then it could be requested again.

- (void)viewDidLoad {
  textComments.delegate=self;
NSString *path =[[[NSBundle mainBundle] bundlePath]
               stringByAppendingString:@"/demo.html"];
[textComments loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
}

-(void)webViewDidFinishLoad:(UIWebView *)_textComments{
  NSString * html_string = [NSString stringWithFormat:@"document.getElementById('editor1').innerHTML='%@';"
                            , self.commentHTML];
  [_textComments stringByEvaluatingJavaScriptFromString:html_string];
  [_textComments stringByEvaluatingJavaScriptFromString:@"document.getElementById('editor1').focus();"];
}

demo.html is an HTML file with a textarea:

<textarea cols="80" id="editor1" name="editor1" rows="8">Content goes
here. ERROR: innerHTML not replaced!</textarea>

The idea is to replace the <textarea> value with the content of the variable self.commentHTML .

The first time the Table View Controller loads, it works fine replacing the value of <textarea> with self.commentHTML variable.

Upon user CLOSE action, another stringByEvaluatingJavaScriptFromString happens to get the innerHTML value and it works fine as well, then I make sure that I dismiss the UIWebView outlet:

[textComments stopLoading];
[textComments removeFromSuperview];
textComments.delegate=nil;
self.textComments=nil;

So far, so good. The problem is when the User launches the View Controller again, UIWebView loads with the pre-loaded demo.html content, but the JavaScript stringByEvaluatingJavaScriptFromString on webViewDidFinishLoad is not happening. I already made sure that webViewDidFinishLoad is being fired. I also made sure that didFailLoadWithError is not being fired also.

Any ideas of why the second time that the View Controller is fired, the stringByEvaluatingJavaScriptFromString is NOT happening?

I'm wondering if I have to destroy/dealloc/discharge something else that I'm not envisioning here upon CLOSE.

Thanks so much for any help.

Edit to Include the full demo.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>CKEditor</title>
    <meta content="text/html; charset=utf-8" http-equiv="content-type" />
    <script type="text/javascript" src="ckeditor.js"></script>
</head>
<body><p><textarea cols="80" id="editor1" name="editor1" rows="8">Content goes here. ERROR: innerHTML not replaced!</textarea></p>
</body>
</html>

这里没有足够的代码来确保,但是我的第一个猜测是该序列使用<textarea id="editor1">创建了dom的两个独立元素,并且javascript确实执行了,但是将内容放入了错误的textarea 。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM