Problems with implementing JavaScript code in a WebView

I am currently working on an android WebView project where I have managed to change the background color to orange with this code snippet.

@Override
                public void onPageFinished(WebView view, String url) {
                    wv.loadUrl("javascript:(function() { " +  
                            document.body.style.background = 'orange'; 
                    "})()");
                }

Whenever the desired page is loaded, the background successfully changes to orange.

However, my attempt to hide a button on the same page using the following code does not seem to work:

@Override
                public void onPageFinished(WebView view, String url) {
                    wv.loadUrl("javascript:(function() { " +  
                    "var input = document.getElementById('submit');" +
                    "input.style.display= 'none';" +  
                    "})()");
                }

The button remains visible despite the code above. Any advice on what mistake I might be making?

Answer №1

In my opinion, the issue lies in the fact that onPageFinished is triggered once the page finishes loading from the network, which may not coincide with the DOM being fully prepared. Have you checked for any JavaScript errors in logcat?

You might have better success by attaching the JS function to an onload event listener.

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

Vertical alignment of buttons in Cordova App Rate

Currently, I am utilizing phonegap along with the app rate plugin. When I execute the code: AppRate.promptForRating(true); All buttons are appearing in a singular line. Despite there being 3 buttons available, the text on the buttons is getting truncate ...

Solutions for resolving the issue of not being able to load images when using merged-images in JavaScript

I have a base64 image here and it has already been converted. data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxQSEhUTEhQWFhUXGBoaGBgYGBcXGhgXGBgYGhgbHhoZHiggGholHhgYITEhJSkrLi4uHR8zODMtNygtLisBCgoKDg0OGhAQGi0lICUtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0 ...

What is the process of dynamically loading CSS into an HTML document?

In my C# program, I am utilizing a web browser control and setting its HTML property programmatically by loading it from an HTML string variable. While this setup works almost perfectly, I have noticed that it loses the reference to the CSS file. I believe ...

What steps can I take to bring this idea to life in my gallery?

Currently, I am facing a roadblock while transitioning my design concept into actual code. My main concern lies with creating this art piece. Although the gallery is up and running with all the images in place, I'm encountering difficulties with the s ...

Using assertThat for comparing dates

Looking to create a test for a search functionality involving dates. I am considering writing the test code like this: assertThat(repository.findByBookingDateAfter(LocalDate.of(2016, 1, 1))).extracting("bookingDate").are(...)); The structure of the class ...

Inserting an item into a list

Looking for assistance with this particular scenario: { "EekvB3cnwEzE":{ "name":"hi", }, "Brv1R4C6bZnD":{ "name":"yup", }, "kRwRXju6ALJZ":{ "name":"okay", } } I'm attempting to store each of these objects in an array. Howe ...

Assign the source property of the Handsontable dropdown option to match the related data value

I received a dynamic data array through an ajax call: data=[ // values are subject to change ['Alex', '16', ['2024-01-01,50000', '2024-03-01,20000', '2024-05-01,30000']], ['Bob&apos ...

Executing AngularJS Accordion event on a webpage

Hello, I am new to using AngularJS and I am currently working on implementing an accordion feature. The accordion is used to display a list of channels on the site. However, I am encountering an issue with the implementation. I want the accordion to be hi ...

How to Trigger a Django View Using Ajax

I am working on integrating Ajax with Django to trigger an action upon button click. I have successfully invoked the JavaScript function, but I am facing issues in calling the Django view. Despite no errors being displayed, the print statement within my vi ...

The excess triggering of ajax events is occurring due to the else clause

My website contains an ajax function that calls a cgi script. Occasionally, this script returns a 500 error. I want to modify the AJAX call so that it retries the request when this error occurs. Here is how I have attempted to do so: function shelverx() { ...

Express displays HTML code as plain text

I am currently facing an issue where I am trying to display an html table on /guestbook.ejs and then redirect it to /guestbook. However, the content of my guestbook.ejs file is being displayed as plain text rather than rendering the HTML code. Below is th ...

Handsontable: How to update renderers when a row is deleted

Implementing Handsontable into our reporting system has been a success, except for one issue. I am using renderers to highlight error cells by setting the background color to red. However, when I remove a row using the context menu's "remove row" opti ...

Which hook should be implemented for automatically updating stock levels after a successful payment on WooCommerce?

When working with WooCommerce, I have implemented my own custom payment method using javascript code. I have successfully retrieved the total amount and passed it to my payment system using the following PHP code: <?php $GLOBALS['cart_total'] ...

JSON placeholders for variable substitution

I'm seeking a Java library that can seamlessly substitute variables when converting Json to an Object in real-time. Imagine a Json template with placeholders for variable substitution: { "User": { "Name": "${name}", "Age": ${age} } } On ...

Error message: Unable to locate file gst/gst.h - fatal error encountered

I am having trouble with importing the gstreamer projects for my Android application. I followed the tutorials on , but I encountered some problems. The Error Messages: fatal error: gst/gst.h: No such file or directory GStreamer cannot be resolved ...

Tips for positioning divs on top of an image with Twitter Bootstrap

I'm having an issue with displaying an image and dividing it using bootstrap div columns. The problem is that the image is overlapping the divs, making it impossible to click or attach jQuery events to it. Here is the code I am currently using: #view ...

Why does Javascript execute in this specific order?

I'm puzzled as to why JavaScript behaves in a certain way, or if I made an error in my code. How is it that the code after $.getJSON runs before the callback completes? window.ratingCallback = function(data){ if(data[0].code == 3){ ratin ...

Using regular expressions to extract substrings from URL

I have different URL routes, such as var url = '/product' and '/product/create'. Is there a way to use Regex in order to extract only the route name like 'product'? ...

Creating a delayed queue using RxJS Observables can provide a powerful and

Imagine we have a line of true or false statements (we're not using a complicated data structure because we only want to store the order). Statements can be added to the line at any time and pace. An observer will remove items from this line and make ...

Error: Cannot access WeightedGraph due to a null pointer

Currently, I am working on implementing Dijkstra's algorithm to calculate the shortest path between two vertices based on the coordinates imported from a file. The algorithm works fine for adding vertices but encounters a null pointer exception when t ...