我有这个WebView,我想在其中打开这个脚本。我该怎么做?
WebView webview = (WebView) findViewById(R.id.webView1);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("javascript:<script>http://ad.leadboltads.net/show_app_ad.js?section_id=xxxxxxxxx</script>")
这是脚本:
<script type="text/javascript" src="http://ad.leadboltads.net/show_app_ad.js?section_id=xxxxxxxx"></script>
答案 0 :(得分:1)
尝试这样,我没有测试过它可能会有轻微的错误。但它应该给你这个想法:
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url){
webview.loadUrl("javascript:(function() { " +
"var script=document.createElement('script');" +
"script.type='text/javascript';" +
"script.src='http://ad.leadboltads.net/show_app_ad.js?section_id=xxxxxxxx';" +
"document.getElementsByTagName('head').item(0).appendChild(script);"+
"})");
}
});
webview.loadUrl("http://www.google.com");
答案 1 :(得分:0)
以下是解决方案:
WebView webview = (WebView) findViewById(R.id.webView1);
webview.setVisibility(View.VISIBLE);
webview.bringToFront();
webview.getSettings().setJavaScriptEnabled(true);
webview.setBackgroundColor(Color.TRANSPARENT);
String html = "<script type='text/javascript' src='http://ad.leadboltads.net/show_app_ad.js?section_id=xxxxxxxx'></script>";
webview.loadData(html, "text/html", "utf-8");