最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Javascript function is not getting called in android webview - Stack Overflow

programmeradmin0浏览0评论

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=".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 .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("/");

}

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.

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./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 .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./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.

Share Improve this question edited Dec 8, 2013 at 21:24 BenMorel 36.6k51 gold badges205 silver badges336 bronze badges asked Nov 8, 2013 at 8:08 Qadir HussainQadir Hussain 8,85613 gold badges94 silver badges127 bronze badges 2
  • have you tried without javascript:;? Maybe there is some security block on webview – Luca Rainone Commented Nov 8, 2013 at 8:28
  • @chumkiu this also didn't worked. – Qadir Hussain Commented Nov 8, 2013 at 8:40
Add a ment  | 

4 Answers 4

Reset to default 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.

Change this:

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

To:

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

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>

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);
                }
            });
发布评论

评论列表(0)

  1. 暂无评论