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?

1 Answer 1

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.

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

2 Comments

Unfortunately, this is not my website. I have no control over it=) Is there other workaround?
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: stackoverflow.com/questions/32530228/…

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.