Android WebView always returns null for javascript getElementById on loadUrl

Finally I found a solution! And it is a really strange behaviour.

First of all you need to specify setDomStorageEnabled(true) on your webview. Otherwise the DOM doesn’t work. I wonder why no tutorial gave a hint about. But ok.

myview.getSettings().setDomStorageEnabled(true);

After this I ended up in a white blank page with only the value I set. The strange thing is, that javascript:document.getElementById('myfield').value="aaa"; returns a value. Namely the one I set. So a new blank page was created that only contains the string “aaa”.

I solved it by modifying the javascript to throw away the return result:

javascript:var x = document.getElementById('myfield').value="aaa";

And voilá. It is working.

Leave a Comment