100

In my application I am using WebView and in that I am using JavaScript alert( ) method but its not working, no pop-up appears.

in my manifest file I have added

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

and in activity file I have added

mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/demo.html");

In layout xml file I have added

<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 ?

S1LENT WARRIOR
  • 11,704
  • 4
  • 46
  • 60
user655192
  • 1,011
  • 2
  • 8
  • 5
  • 6
    It's "JavaScript", not "Java Script", "java-script", or "Java script" (all of which appeared in your original question). I've corrected it for you. – T.J. Crowder Mar 11 '11 at 10:49
  • 3
    @T.J.Crowder I would venture to say it should be called *Javascript*. – Kris Selbekk Aug 08 '12 at 08:20
  • 7
    @KrisSelbekk: Nope. :-) "JavaScript" is a registered trademark of Oracle (gotta love mergers and acquisitions), and used (with permission) [by Mozilla](https://developer.mozilla.org/en-US/docs/JavaScript) for their implementation. In both cases, *with* the capital S. That's the only normative form of that name. The other name for the language, of course, is ECMAScript (or ES), but no one really uses it except when talking about [ES5](http://ecma-international.org/ecma-262/5.1/). – T.J. Crowder Aug 08 '12 at 09:11

6 Answers6

135

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.

Stephen Quan
  • 21,481
  • 4
  • 88
  • 75
97

Check this link , and last comment , You have to use WebChromeClient for your purpose.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
sat
  • 40,138
  • 28
  • 93
  • 102
  • 8
    thank you! webView.setWebChromeClient(new WebChromeClient() { @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { return super.onJsAlert(view, url, message, result); } }); – nurnachman Oct 15 '12 at 12:04
  • your comment helped me.. as does the Nikhil's answer below. – Vamsi Challa May 09 '14 at 15:34
  • Works like charm. But then.... does WebClient have any advantage over WebChromeClient. – Josh Sep 01 '15 at 10:38
  • 3
    It may be a stupid question, but anyway: why override a method to only call a super's method with the same params? – Dmitry Andrievsky Oct 29 '15 at 06:25
  • 1
    Don't forget to add `webview.getSettings().setJavaScriptEnabled(true);` – Jemshit May 25 '16 at 13:09
  • @MuhammadSufiyan can u help me my JS is not working in webview alrady tried solution but nothing works – Erum Dec 07 '16 at 10:57
  • @Erum write your question with your tried code so far.. and explain what you want to achieve and what problem you are getting in implementation ... I bet... there's lot of people here to help you out... and I will too try.. to help you out... – Mohammed Sufian Dec 14 '16 at 16:26
  • @MuhammadSufiyan http://stackoverflow.com/questions/41547303/js-confirm-box-in-android-webview-not-working – Erum Jan 09 '17 at 11:43
27
webView.setWebChromeClient(new WebChromeClient() {
    @Override
    public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
        return super.onJsAlert(view, url, message, result);
    }
});
akhilesh0707
  • 6,709
  • 5
  • 44
  • 51
Nikhil Dinesh
  • 3,359
  • 2
  • 38
  • 41
  • This works great, but when I call `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 app – Michael Nov 09 '16 at 18:13
5

The 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");
}
biegleux
  • 13,179
  • 11
  • 45
  • 52
user1645162
  • 51
  • 1
  • 1
0

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);
        }

    });
Tarit Ray
  • 944
  • 12
  • 24
0

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;
            }
    }
AbhinayMe
  • 2,399
  • 1
  • 20
  • 21