1

I have following code in my WebView

<script src="remote src" param1="value1" param2="value2" ... />;

When it runs on Androids, lower than Kitkat, I use typical

webView.loadDataWithBaseURL(..)

and script works fine. But, as we know, in KitKat WebView had absolutely changed, and scripts should be run as

webView.evaluateJavascript(script, callback);

and now is very unclear how to run my script, which is rendered with the rest of the page, in KitKat.

4
  • Have you remembered to call webView.getSettings().setJavaScriptEnabled(true)? Commented Jan 13, 2014 at 12:47
  • Of cource, otherwise code will not be run under Android lower than KitKat Commented Jan 13, 2014 at 12:58
  • For your loadDataWithBseURL() scenario, is the string you are passing into it literally the <script> tag that you have shown? Commented Jan 13, 2014 at 13:08
  • No, it is typical html page, starts from <html><head>..<head><body> Commented Jan 13, 2014 at 13:28

4 Answers 4

2

How about this:

String jScript = "(function() {\n" +
                    "var p=document.getElementsByTagName('head')[0];\n" +
                    "var s=document.createElement(\"script\");\n" +
                    "s.type=\"text/javascript\";\n" +
                    "s.src=\"http://pathtoscript/jscript.js\";\n" +
                    "s.async = true;\n" +
                    "p.appendChild(s);\n" +
                "}\n" +
            "})();\n";

webView.evaluateJavascript(jScript, callback);

But in this case i don't know is there possibility to get a callback.

Sign up to request clarification or add additional context in comments.

Comments

0

The script should still execute fine in KK if it's inline inside the content that you pass to loadDataWithBaseUrl. The new evaluateJavaScript API is intended to execute script in the context of the document currently loaded in the WebView, after it has been loaded.

Prior to KK, developers would call loadUrl passing in a javascript: URL to achieve the same effect. The new API simplifies this, and allows a callback to be executed when the script has been run.

Hope this helps to clarify.

2 Comments

The script should still execute fine But it doesn't execute!! Just in KitKat
Ok. Can you share the script? If not, try remote debugging in case there are any errors being thrown - see developers.google.com/chrome-developer-tools/docs/…
0

Just looked into supporting loading of remote JS.

I have tried the following:

Loading a file through loadUrl("file://....") with an absolute path for a remote js file i.e.

Loading a html string into the webview with an absolute path for a remote js file

mWebView.loadDataWithBaseURL("content://com.example.remotejs/", htmlString, "text/html", "utf-8", null);

Loading a html string with a valid base url and a relative remote js file

mWebView.loadDataWithBaseURL("http://labs.gauntface.co.uk/", htmlString, "text/html", "utf-8", null);

Script tag:

All of the above worked. Can you please provide an example - my source is hosted here: https://github.com/gauntface/QuickWebViewExamples/tree/master/RemoteJSTest

2 Comments

Useless comment. My question is about how to execute scrit with remote src. I cannot cut it and put is as a parameter of a method evaluateJavascript()
Sorry I misunderstood. So you are loading a html file or string into the WebView and loading javascript similar to: <script src="mysite.com/somejs.js" param1="value1" param2="value2" ... />; Where the 'mysite.com" was loaded as the base parameter of loadBaseUrl?
0

Solution is simplier.

Problem is now ith KitKat, but with Github

https://stackoverflow.com/a/18049842/1346120

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.