Add javascript into WebView

╄→尐↘猪︶ㄣ 提交于 2019-12-28 05:42:04

问题


I am developing an application where in 1 part I want to add javascript into WebView.. But am not getting how to do it in an appropriate way.. Can anyone pls guide me into this?????

I am doing it like:

      wb=(WebView)findViewById(R.id.webView1);
        wb.getSettings().setJavaScriptEnabled(true);
        wb.getSettings().setPluginState(WebSettings.PluginState.ON);
        wb.getSettings().setPluginsEnabled(true);

        wb.loadUrl("javascript:<script " ></script> ");
        wb.setWebViewClient(new HelloWebViewClient());


 public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

回答1:


It was very simple..also without using any javascriptInterface..In my code, Instead writing:

 wb.loadUrl("javascript:<script>   </script>");

use,

  wb.loadDataWithBaseURL(null,"<script>   </script>","text/html","utf-8",null);

and its working now :)




回答2:


Look at these nice tutorials about how to implements javascript in webview in android..

I think its provides you all the information what you needed..

Android WebView

Android WebView, Javascript and CSS

EDIT: Further if your implemented code having any exception or not working then please post that code and exception then here we can help you..



来源:https://stackoverflow.com/questions/8533187/add-javascript-into-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!