My favorites | Sign in
Project Home Issues
New issue   Search
for
  Advanced search   Search tips   Subscriptions
Issue 752: JavaScript alert() calls do not execute in WebView
14 people starred this issue and may be notified of changes. Back to list
Status:  Declined
Owner:  ----
Closed:  Jul 2009


Sign in to add a comment
 
Reported by joelhull...@gmail.com, Jun 18, 2008
The following code does nothing in a WebView.

alert("display this text");

The WebView instance was created like this...

startPage = "http://10.0.2.2:8080/testwebview";
webView = (WebView) findViewById(R.id.WebView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.clearCache();
webView.loadUrl(startPage); 
Jun 18, 2008
#1 joelhull...@gmail.com
More problems are seen when the html is loaded from the file system.
startPage = file:///android_asset/wvtest.html
Jun 18, 2008
#2 joelhull...@gmail.com
Also, images do not always display when the page is first loaded and the image is 
inserted dynamically using javascript and innerHTML.

See attached html file.
wvtest.html
533 bytes   View   Download
Jul 8, 2008
#3 Brett.Ch...@gmail.com
I'm not sure your syntax is correct, I don't think a html file can loaded from either
URL you provide above. Have you tried to push your wvtest.html to the /sdcard
directory on the emulator, and load it from there?
Status: NeedsInfo
Cc: Brett.Chabot
Jul 12, 2008
#4 joelhull...@gmail.com
The html does load and execute just fine.  There is other JavaScript in the html 
file that is execute but all the alert statements are not executed.  No alert dialog 
appears.  It seems like the JavaScript engines is ignoring them or it does have a 
handle to the window so it can't create the dialog.

The 10.2.2.2 IP address if for localhost.  This must be used with the emulator since 
the emulator uses 127.0.0.1 itself.  If you run a local webserver you can hit it 
with this code easily and load an html file yourself to try it out.
Dec 23, 2008
#5 baseball...@gmail.com
I can comfirm this issue - just experienced the same problem, googled it and ended up
here.
Feb 5, 2009
#6 jbq+legacy@google.com
(No comment was entered for this change.)
Status: New
Mar 1, 2009
#7 rgro...@gmail.com
I can confirm this issue as well.
Javascript alert() windows do not popup from a webview.
May 21, 2009
#8 gnroses....@gmail.com
i am also facing the same thing . its not popping up alert 
May 23, 2009
#9 stevenc...@gmail.com
I had this issue too with xcode Simulator iPhone OS 2.1.  I installed v2.2.1, and it
works now.
Jul 23, 2009
#10 jlape...@google.com
You must provide a chrome client that can handle alerts:

file:///home/jlapenna/src/android/sdk/docs/reference/android/webkit/WebChromeClient.html#onJsAlert(android.webkit.WebView,%20java.lang.String,%20java.lang.String,%20android.webkit.JsResult)
Status: Declined
May 9, 2010
#11 chabbydr...@gmail.com
(No comment was entered for this change.)
Cc: -Brett.Chabot
Jan 19, 2011
#12 vineetha...@gmail.com
One should use a WebChromeClient to get these alerts and the functionality can be overwritten using the onJsAlert() method. Hope this helps!

WebView wv=new WebView(this);	
wv.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);
       }
Apr 5, 2011
#13 vikaskan...@gmail.com
Above event worked but, it does not call on first alert call, next all the alert calls works perfectly.. Is there any work around this issue?
Jun 13, 2011
#14 nirav1...@gmail.com
yupppi...i have dont it with setWebChrmeClient...thank u
Dec 9, 2011
#15 suri2...@gmail.com
I have same issue that javascript is not working in android device. even for alert function. I have added that in body to fire onload page. but it didnt display anything. what exactly have to do in The WebView instance or WebChrmeClient. Please any one can explain?
Dec 13, 2011
#16 christop...@gmail.com
@comment 15 : see the comment 12 (https://code.google.com/p/android/issues/detail?id=752#c12)

add this code in the onCreate of your WebViewActivity. This will solve your problem.
Dec 30, 2012
#17 cheemafa...@gmail.com
I don't get it. Why do we need to override the onJsAlert() function to execute the DEFAULT behavior? :s  I mean shouldn't Android be handling all the default behaviors itself?
Mar 21, 2013
#18 squan...@gmail.com
You don't need to override the onJsAlert() function.
You need to supply a WebChromeClient(), the default one will do, i.e.

WebView wv = new WebView(this);	
wv.setWebChromeClient(new WebChromeClient());

Apr 9, 2013
#19 pnavee...@gmail.com
thnkyou  very much squan..@gmail.com ..i got the solution...:-)
Dec 10, 2013
#20 ashokred...@gmail.com
I have used the following code to make javascript alert in webview.
WebView wv = new WebView(this);	
wv.setWebChromeClient(new WebChromeClient());

The issue with above is when the javascript alert shown on webview ,once the user press home button ,then if i tried to open the application second time the webview is not displaying. any idea?
Jun 26, 2014
#21 PrakashR...@gmail.com
Thank u @squan its works well........ :-)
Sign in to add a comment

Powered by Google Project Hosting