0

I want to change height of a field in webView(Android). I tried doing it using a javascript but it doesn't work.

webView.loadUrl("javascript:(var field = document.getElementById("targetdiv");field.style.height='50px;);");

I've also enabled javascript:

WebSettings ws = webView.getSettings();
ws.setJavaScriptEnabled(true);

Still it doesn't work. Please help me out !

Gaurav Arora
  • 16,016
  • 5
  • 31
  • 42

1 Answers1

1

This works along like this

myWebView.loadUrl("javascript:showString('Hello World!')");

where showStringis a function that outputs the given string (not shwon here).

Thariama
  • 47,807
  • 11
  • 127
  • 149
  • I'm new to javascript. I've written one: webView.loadUrl("javascript:(var field = document.getElementById ("targetdiv");field.style.height='50px;);"); Can u help me in correcting it. Thanks ! – Gaurav Arora Oct 17 '12 at 12:04
  • this will set the variable, but there will be no output - but you have (probalby) executed javascript in webView – Thariama Oct 17 '12 at 12:05
  • for a hello World programm see this thread: http://stackoverflow.com/questions/4325639/android-calling-javascript-functions-in-webview – Thariama Oct 17 '12 at 12:06
  • you will need to define a function: function switchValues(){"...your code here..."}; and execute this function using mWebView.loadUrl("javascript:switchValues"); – Thariama Oct 17 '12 at 12:14
  • You mean I need to define function in HTML. But I'm getting HTML from server and its not defined locally. – Gaurav Arora Oct 17 '12 at 12:17
  • Its not possible at my end. Is there any other way I could achieve this. Thanks ! – Gaurav Arora Oct 17 '12 at 12:22
  • there might be, but there is none i know of - sry – Thariama Oct 17 '12 at 12:25