5

I'm making an app which runs in android webview. I'm facing a strange issue, My JavaScript function is not getting called on <a></a> onClick method.

Here is my html and JavaScript code:

<html>

<script type="text/javascript" src="http://blue.xxxxxxxx.com/xxxxx/site/js/jquery.js"></script>
<script>
function openFileDialog()
{
    //$("#file").click();   
    alert("Test");
}
</script>
<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>


<a href="javascript:;" onclick="openFileDialog();">Shujaat</a>
</body>
</html> 

and here is my whole java webviews code.

package com.example.findozerapp;

import my.functions.MyFunctions;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebSettings.ZoomDensity;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {
Context context = MainActivity.this;
Activity activity = MainActivity.this;

WebView webView;
MyFunctions myFunctions;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myFunctions = new MyFunctions(activity);
    webView = (WebView) findViewById(R.id.webView1);

    configureWebview();

}

@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();

    webView.loadUrl("http://www.xxxxxxx.com/qadir/");

}

private void configureWebview() {

    webView.setPadding(0, 0, 0, 0);
    webView.setInitialScale(myFunctions.setWebViewScale());
    webView.getSettings().setDefaultZoom(ZoomDensity.FAR);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setCacheMode(MODE_APPEND);

    webView.setWebViewClient(new MyWebViewClient());

}

private class MyWebViewClient extends WebViewClient {

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub
        return super.shouldOverrideUrlLoading(view, url);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);
    }
}

 }

Notice I have enable javascript in my webview.

but its not calling my javascript function openFileDialog().

and one thing more. Whenever I load my this webpage in default android browser its working perfectly. Where i have did wrong. Please check my webview's settings.

2
  • have you tried without javascript:;? Maybe there is some security block on webview Commented Nov 8, 2013 at 8:28
  • @chumkiu this also didn't worked. Commented Nov 8, 2013 at 8:40

4 Answers 4

3

If you've set your targetSdkVersion to 17 or higher, you must add the @JavascriptInterface annotation to any method that you want available your web page code (the method must also be public). If you do not provide the annotation, then the method will not accessible by your web page when running on Android 4.2 or higher.

Sign up to request clarification or add additional context in comments.

2 Comments

Im not getting you, where should I add JavascriptInterface annotation?
0

Change this:

<a href="javascript:;" onclick="openFileDialog();">Shujaat</a>

To:

<a href="javascript:void(0)" onclick="openFileDialog()">Shujaat</a>

2 Comments

then this is not the problem, you must have anything else that is causing the issue, place an alert at the start of your javascript function openFileDialog() this will give you a clue whether your function is being called or not. And still doen't work then try to catch the bug in console view of your firebug extension.
Yes I have tested it in Mozila , chrome, and my android default browser, its working perfectly. but in android webview controll its not getting invoked
0

Try this:

In your java code (before webView.setWebViewClient(new MyWebViewClient());:

webView.setWebChromeClient(new WebChromeClient());

In your HTML code:

<a href="javascript:openFileDialog()">Shujaat</a>

Comments

0

Try by setting the Chrome client to webview and Override the onJsAlert method in the Chrome client like below.

            webView.setWebChromeClient(new WebChromeClient() {
                @Override
                public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                    return super.onJsAlert(view, url, message, result);
                }
            });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.