Add Javascript to Webview on Android

I want to add a Leadbolt ad to a webview. Here is the ad code below

<script type="text/javascript" src="http://ad.leadboltads.net/show_app_ad.js?section_id=xxxxxxxxx"></script> 

How can i add this?

+4
source share
2 answers

WebView has a loadData () method. Allow webview to download html code directly. You can try.

 String js_str = "<script type='text/javascript' src='http://ad.leadboltads.net/show_app_ad.js?section_id=xxxxxxxxx'></script>" WebView webview = new WebView(); webview.loadData(js_str, null, null); 
+3
source

try it

 WebView wv = (WebView)findViewById(R.id.webViewad); wv.getSettings().setJavaScriptEnabled(true); StringBuilder htmlData = new StringBuilder("<html>"); htmlData.append("<head><title>Ad</title></head>"); htmlData.append("<body>"); htmlData.append("<script type=\"text/javascript\" src=\"http://ad.leadboltads.net/show_app_ad.js?section_id=YOUR_ID\"></script>"); htmlData.append("</body>"); htmlData.append("</html>"); wv.loadData(htmlData.toString(),"text/html", "ISO 8859-1"); 
0
source

Source: https://habr.com/ru/post/1432191/


All Articles