Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript sometimes doesn't work in android's webview

I put a webview in my application to visit a webpage which includes some javascript functions, my purpose is when users click a link in the webpage, it will start a new activity in the application. I've written "webSettings.setJavaScriptEnabled(true);" in my source codes.

It works well in most of the time, however sometimes it doesn't work, there is no response when users click, and I don't know why because the environment is not changed at all. Has anybody know why?

like image 327
Garfex Avatar asked Jul 22 '10 07:07

Garfex


People also ask

How do I enable JavaScript on Android WebView?

Enable JavaScript JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView . You can retrieve WebSettings with getSettings() , then enable JavaScript with setJavaScriptEnabled() . WebView myWebView = (WebView) findViewById(R.

Does JavaScript work on Android studio?

Can we use JavaScript for Android? Yes, of course! The Android ecosystem supports the concept of hybrid apps, which is a wrapper over the native platform.

What can I use instead of WebView?

Alternatives to WebView If you want to send users to a mobile site, build a progressive web app (PWA). If you want to display third-party web content, send an intent to installed web browsers. If you want to avoid leaving your app to open the browser, or if you want to customize the browser's UI, use Custom Tabs.

How can I improve my WebView performance?

I read about how to increase performance of WebView by implementing Caching web resources like JS, CSS and image files. You can also static resources in your native application, and by intercepting the Resource requests you can override the default behaviour of WebView.


2 Answers

v.setWebChromeClient(new WebChromeClient() {
    @Override
    public void onConsoleMessage(String message, int lineNumber,String sourceID) {
        Log.d("MyApplication", message + " -- From line "+ lineNumber + " of " + sourceID);
        super.onConsoleMessage(message, lineNumber, sourceID);
    }
});

check this code so at least you will get error message and one more thing is that android doesn't supports all JavaScript functions.

like image 159
Brijesh Masrani Avatar answered Oct 16 '22 18:10

Brijesh Masrani


I had the same issue. I changed the event for from 'click' to 'touchstart' and now its awesome.

like image 36
Imran Omar Bukhsh Avatar answered Oct 16 '22 17:10

Imran Omar Bukhsh