Suresh Subedi
Suresh Subedi

Reputation: 710

How to change style in android webview by injecting javascript?

When I inject document.getElementsByTagName("body")[0].style.background = "red"; in developers console in firefox, background becomes red. But when I do webview.loadUrl("javascript:document.getElementsByTagName(\"body\")[0].style.background = \"red\";"); in android webview, whole content in it gets replaced by the word "red". Why is background color not changing and content is being replaced? Also, how can I change the style properties in webview using javascript?

Upvotes: 1

Views: 2493

Answers (1)

Suresh Subedi
Suresh Subedi

Reputation: 710

I solved this problem by injecting the following instead:

webview.loadUrl("javascript:(function() {document.getElementsByTagName(\"body\")[0].style.background = \"red\";})()");

Upvotes: 2

Related Questions