The method OnJSAlert in WebChromeClient is failing to execute

I am currently working with a JS script in a WebView, where the script triggers an alert message to the WebView that I want to capture in my app. However, I am facing an issue where the onJSAlert method is not being called and I am unable to use the @Override annotation when defining the method.
The imports in my code include -

import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.JsResult;
import android.webkit.WebSettings;

The WebView setup looks like this -

webView = (WebView)findViewById(R.id.webView1); 
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
            Log.d("fibi", "finished loading");
            }
        }
        //@Override //If I uncomment this I get compilation error
        public boolean onJSAlert(WebView view, String url, String message, final JsResult result) {
            encResult = message;
            Log.d("fibi", encResult);
            result.cancel();
            return true;                
        }
    });
webView.loadUrl("file:///android_asset/test.html");

The HTML code for test.html is as follows -

<html>
   <body >
     <div onclick="alert('CLICK')"> Click Me !!  </div>
   </body>
</html>

Upon running the app, the HTML file loads successfully and the onProgressChanged method logs "finished loading". However, clicking on the link within the loaded page does not trigger the onJSAlert method - instead, a dialog window appears on the device displaying the alert message.
Attempting to add the @Override annotation to the onJSAlert method results in an error stating "The method onJSAlert(WebView, String, String, JsResult) of type MainActivity.MyWebClient must override or implement a supertype method".
Any suggestions?

Answer №1

Incorrect spelling of a method name will result in unsuccessful method calls

onJSAlert

Corrected to:

onJsAlert

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Uploading Images Dynamically with Ajax and jQuery in PHP

[Resolved Previous Issue] Recently, I obtained an image upload script that utilizes Ajax and jQuery. My goal is to restrict the maximum number of uploads to just 8 images. Any suggestions or advice would be greatly appreciated. Source of Download: ...

Create a form action dynamically with JavaScript

html code : <section class="main"> <form class="form-4" name="form4" id="form4" method="POST"> <p class="submit"> <button type="submit" name="submit" onclick="show()"><i class="icon-thumbs-up icon-large"></i> ...

Node.js allows for the halting of statement execution if it exceeds a predetermined time limit

Is there a way to pause the execution of a statement if it exceeds a certain time limit? I need help with this problem. In the code snippet below, if the statement const result = await curly.get('www.google.com'); takes longer than 2 seconds to ...

Create a custom npm package that is compatible with frontend environments like create-react-app. Ensure you have a suitable loader in place to handle the specific file type of the module

After developing a node module and releasing it as a node package, I encountered an issue when trying to use it in frontend applications (specifically with a 'create-react-app'). The error message that appeared stated: Module parse failed: Unexp ...

Is there a way to enclose a portion of text within an element using a span tag using vanilla JavaScript?

Imagine having an element like this: <p id="foo">Hello world!</p> Now, the goal is to transform it into: <p id="foo"><span>Hello</span> world!</p> Is there a way to achieve this using pure vanilla J ...

Collaborating on search suggestion functionality

In my search suggestion script, the results are dynamically updated as the user types. My goal is to make it so that when a user clicks on a suggestion, they are redirected to the following page: searchPage.php?user_query=what the user has typed in the inp ...

What is the appropriate situation to utilize the OnCompleteListener() method for queries?

Currently developing a small app using Firebase, I discovered that setting Persistence(true) will backlog requests and carry them out once users regain connectivity. However, I encountered issues with placing my code inside OnCompleteListener, as it does n ...

JavaScript - Function is not recognized even though it has been defined, following the completion of a jQuery function

I am encountering an 'Is Undefined' error when running a piece of JavaScript code. https://i.sstatic.net/q3EfY.png < script language = "javascript" type = "text/javascript" > // function added for click on submit - dp0jmr 05/23/20 ...

Navigating with React Router using URL parameters

After implementing react router with a route path taskSupport/:advertiserId that includes parameters, I encountered an issue when trying to access the link http://localhost:8080/taskSupport/advertiserId. My browser kept returning 404 (Not found) errors for ...

What is the best way to eliminate backslash escaping from a JavaScript variable?

I am working with a variable called x. var x = "<div class=\\\"abcdef\\\">"; The value of x is <div class=\"abcdef\"> However, I want it to be <div class="abcdef"> Can someone help me unescape ...

Guide to dynamically incorporating items from an array into the filter operation

How can I dynamically insert items from an array into the filter on the wizard.html page, so that when the templateUrl in the route is called, it can be rendered on the wizard.html page? Controller angular.module('tareasApp') .controller(&apo ...

Utilizing AJAX to dynamically populate a dropdown menu with data from a SQL

Having recently delved into the world of ajax, I have a basic understanding of how it works. I've implemented a registration form that dynamically updates the fields for "country, city, and area" using ajax to fetch data from a database. The PHP code ...

Initiate an Ajax call to pre-fetch video content

I've been attempting to download a video from my own source and display a loading message while it's being downloaded. Once the download is complete, I want to hide the loading icon and have the video ready for playback. My issue lies in gettin ...

Leveraging xgettext for extracting translatable content from VueJS files

Attempting to utilize xgettext for extracting translatable strings from a VueJS file has presented some challenges. Specifically, xgettext does not seem to recognize JavaScript code within a computed property in VueJS. For instance, consider the following ...

Incorporate a local asciinema file into an HTML document

Is there a way to embed a local asciinema session into an html file? Here is how my directory is structured: $ tree . ├── asciinema │ └── demo.cast ├── css │ └── asciinema-player.css ├── index.html ├── js │ ...

Tips for resolving the chakra-ui theme issue with loading input background

I've been working on applying a theme to my Next.js app using chakra-ui, but I've been facing an issue that I can't seem to resolve. Currently, when the /form route is loaded, the theme is set to light. After changing it to dark, the theme i ...

Tips on arranging an array based on dates and data in JavaScript?

I have an array with dates and other data that I need to sort based on the dates. I'm unsure of how to go about this. Can someone please assist me? Here is the array in question: 0:{_id: "01-11-2017", CommentCount: 221, Likecount: 141, Followcount: ...

Using Typescript to typecast in D3.js

Utilizing the D3 graph example available here. I've defined my data object as shown below: interface ID3Data { age: string, population: number } const data: ID3Data[] = [ { age: "<5", population: 2704659 }, { age: "5-13", population: 4499 ...

JavaScript does not support the enumeration of programs

I'm currently working on a program that takes user input (library, authorName) and displays the titles of books written by the author. Here is an example library structure: let library = [ { author: 'Bill Gates', title: 'The Road Ah ...

Working with XML files in Node.js

I'm currently running an Arch Linux system with KDE plasma and I have a 50mb XML file that I need to parse. This XML file contains custom tags. Here is an example of the XML: <JMdict> <entry> <ent_seq>1000000</ent_seq&g ...