Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use js method to load js file and pass url as a parameter from java</p> <p>Here is the full working example </p> <p>[Activity.java]</p> <pre><code>public class MainActivity extends Activity { WebView mWebView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWebView = (WebView) findViewById(R.id.webview); WebSettings webSettings = mWebView.getSettings(); webSettings.setJavaScriptEnabled(true); mWebView.addJavascriptInterface(new JavaScriptInterface(), "jsinterface"); mWebView.loadUrl("file:///android_asset/sq.html"); } final class JavaScriptInterface { JavaScriptInterface() { } @JavascriptInterface public void windowLoaded() { mWebView.loadUrl("javascript:loadScript('test.js', 'testing')"); Log.i("browser", "browser loaded"); } } } </code></pre> <p>[sq.html]</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script&gt; onload = function () { window.jsinterface.windowLoaded(); } //JS function loadScript(url, callback) { console.log("loading script " + url); // adding the script tag to the head as suggested before var head = document.getElementsByTagName('head')[0]; var script = document.createElement('script'); script.type = 'text/javascript'; script.src = url; // then bind the event to the callback function // there are several events for cross browser compatibility //script.onreadystatechange = callback; script.onload = callback; // fire the loading head.appendChild(script); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt;replace this &lt;/body&gt; &lt;/html&gt; </code></pre> <p>[test.js] inside assets folder</p> <pre><code>document.body.innerHTML = "&lt;p&gt;testing&lt;/p&gt;"; </code></pre> <p>on successful execution webview will show testing by loading test.js dynamically.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload