Skip to content

Instantly share code, notes, and snippets.

@stanwu
Last active January 13, 2016 04:28
Show Gist options
  • Select an option

  • Save stanwu/e379feeedbdd3a5475a0 to your computer and use it in GitHub Desktop.

Select an option

Save stanwu/e379feeedbdd3a5475a0 to your computer and use it in GitHub Desktop.
Appending to HTML (concatenation) as String

#Android#

Appending to HTML (concatenation) as String joining is not a good solution, but you can try this approach which will ensure that the html document's sanctity is not broken. Since you have the HTML document with you, it signifies you have control over that document and hence you should be able to do this which I am mentioning below.

  1. Create a function in HTML
function appendText(extraStr) {
     document.getElementsByTagName('body')[0].innerHTML = document.getElementsByTagName('body')[0].innerHTML + extraStr;
}
  1. On WebView load call this function
myWebView.loadUrl("javascript:appendText('extra text or html here')");

this should put it in the onPageFinished()

 public void onPageFinished(WebView view, String url) {
     view.loadUrl("javascript:appendText('extra text or html here')");
 }
  1. Don't forget to add this
myWebView.getSettings().setJavaScriptEnabled(true);`

#QT#

webView->page()->mainFrame()->evaluateJavaScript("document.body.innerHTML += [your code]");

Using scripts you can dynamically "talk" with the HTML DOM.

in the [your code] i'm using a "window.bridge.code" with addToJavaScriptWindowObject.

But you can simples put your text there, or use "document.body.innerHTML += '" + code + "';";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment