Dalam aplikasi saya, saya menggunakan WebView
dan saya menggunakan JavaScript alert( )
metode tetapi tidak berfungsi, tidak ada pop-up yang muncul.
dalam file manifes saya, saya telah menambahkan
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
dan dalam file aktivitas telah saya tambahkan
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/demo.html");
Dalam file layout xml saya telah menambahkan
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Any clue how to enable full JavaScript
in WebView
?
Update
Thanks mark
the alert()
method in the html
file are working now :) .
Now there are two issues in WebView
:
1: I am using a <textarea>
in the html file that i am loading in WebView
, and trying to write in Hindi language font in it, but when i try to write Hindi text it displays as symbols ( rectangle symbols like [] ) .
when i do the same in firefox browser on desktop it works fine.
any clue how to give support for multiple language in textarea
in WebView
?
2: When I am clicking submit and trying to open the value of text in alert()
method in another java script it doesn't work , does it mean even after using WebChromeClient
its applicable only for current loaded html page and not javascripts
called from that page ?
sumber
Jawaban:
Check this link , and last comment , You have to use
WebChromeClient
for your purpose.sumber
webview.getSettings().setJavaScriptEnabled(true);
As others indicated, setting the WebChromeClient is needed to get
alert()
to work. It's sufficient to just set the default WebChromeClient():mWebView.getSettings().setJavaScriptEnabled(true); mWebView.setWebChromeClient(new WebChromeClient());
Thanks for all the comments below. Including John Smith's who indicated that you needed to enable JavaScript.
sumber
mWebView.getSettings().setJavaScriptEnabled(true);
before the linemWebView.setWebChromeClient(new WebChromeClient());
webview.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() { @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { return super.onJsAlert(view, url, message, result); } });
sumber
prompt
from javascript, instead of displaying the prompt text it says, "The page at https://... says:" totally breaking the immersion of the WebView being embedded in the appThe following code will work:
private WebView mWebView; final Activity activity = this; // private Button b; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWebView = (WebView) findViewById(R.id.webview); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setDomStorageEnabled(true); mWebView.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { activity.setProgress(progress * 1000); } }); mWebView.loadUrl("file:///android_asset/raw/NewFile1.html"); }
sumber
You can try with this, it worked for me
WebView wb_previewSurvey=new WebView(this); wb_previewSurvey.setWebChromeClient(new WebChromeClient() { @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { //Required functionality here return super.onJsAlert(view, url, message, result); } });
sumber
if you wish to hide URL from the user, Show an AlertDialog as below.
myWebView.setWebChromeClient(new WebChromeClient() { @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { Log.d(TAG, "onJsAlert url: " + url + "; message: " + message); AlertDialog.Builder builder = new AlertDialog.Builder( mContext); builder.setMessage(message) .setNeutralButton("OK", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int arg1) { dialog.dismiss(); } }).show(); result.cancel(); return true; } }
sumber