0

Enabled Javascript on WebView. Because without JavaScipt web page image scaling is not working properly. This is my code how I am loading url in WebView:

public class MainActivity extends AppCompatActivity {

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

        final String url = "http://nuz.uz/ekonomika-i-finansy/18651-v-2017-godu-v-uzbekistane-prekratitsya-realizaciya-lampochek-ilicha.html";

        final WebView webview = (WebView) findViewById(R.id.webview);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WebView.setWebContentsDebuggingEnabled(true);
        }
        webview.getSettings().setJavaScriptEnabled(true);
        webview.setWebChromeClient(new WebChromeClient() {
            public void onConsoleMessage(String message, int lineNumber, String sourceID) {
                Log.d("Tag", message + " -- From line "
                        + lineNumber + " of "
                        + sourceID);
            }
        });
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String loadingUrl) {
                if (loadingUrl.equals(url)) return false;//open news link in webview
                else {
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(loadingUrl));
                    startActivity(browserIntent);
                    return true;
                }
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){
                Log.d("Tag", "Error occured: "+error.toString());
            }

        });
        webview.loadUrl(url);

    }

}

When I am running app, it is opening web page. After loading all content of page, app is crashing. I tried to show error on console it is showing following error:

12-14 02:42:06.936 25760-25760/? D/Tag: Uncaught TypeError: Cannot call method 'init' of undefined -- From line 951 of http://nuz.uz/ekonomika-i-finansy/18651-v-2017-godu-v-uzbekistane-prekratitsya-realizaciya-lampochek-ilicha.html

I have checked url's content and found following which is causing app crash :

<script type="text/javascript">

    jQuery(document).ready(function () {

        App.init();
    });
</script>

line 951: App.init();

How I can solve this problem without turning off JavaScript? How to prevent WebView from crashing app?

Joe Rakhimov
  • 4,713
  • 9
  • 51
  • 109

1 Answers1

0

If you look at the source code of the page, right above line 951, you'll find

<script type="text/javascript" src="/templates/nuz_uz/assets/js/app.js"></script>

This script declares the App variable to be the result of the anonymous function, but the function doesn't return anything. Therefore App is still undefined at the time jQuery fires the ready event. You basically need to fix the error on the website, your Android app is fine.

  • Unfortunately, this is not my website. I have no control over it=) Is there other workaround? – Joe Rakhimov Dec 13 '16 at 22:35
  • You would have to a) load the website without JS, b) go in and fix the error (not sure if you can mess with the loaded HTML) then c) enable JS. Not feasible or practical in my book. Try this maybe: http://stackoverflow.com/questions/32530228/android-api-23-webview-how-to-ignore-javascript-errors –  Dec 13 '16 at 22:43