Updating HTML in WKWebView with Javascript

I'm using a WKWebView with a Javascript to add items to the webpage, like so:


<!-- test.html -->
<!doctype> 
  <html> 
    <head> 
      <script type="text/javascript"> 
        function addLogItem() { 
          document.getElementById("logItems").innerHTML += "New log item<br>" 
        } 
      </script> 
    </head> 
    <body> 
      <div id = "logItems"> Log items:<br> </div> 
    </body> 
  </html>


func viewDidLoad() {        
     webView.navigationDelegate = self
     let url = Bundle.main.url(forResource: "test", withExtension: "html")!
     webView.load(URLRequest(url: url))
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
     webView.evaluateJavaScript("logItems()") { _, _ in
          self.webView.evaluateJavaScript("document.body.innerHTML") { result, _ in
               print(result!) // Prints correct HTML data, with added "New log item<br>" line
          }
     }
}


The first evaluateJavaScript call invokes the addLogItem Javascript method, the second one confirm the new log item has been added on the HTML page.


However, the WKWebView in the view controller doesn't update and doesn't show the line added line.


How do I get WKWebView to show the new updated content?

Answered by PuzzleMaster in 245537022

Mystery solved. I somehow managed to have a second invisible web view in the view controller and called the javascript methods in the invisible one.

Accepted Answer

Mystery solved. I somehow managed to have a second invisible web view in the view controller and called the javascript methods in the invisible one.

Updating HTML in WKWebView with Javascript
 
 
Q