Troubles arise with JavaScript CSS override functionality when specifically targeting Android API 19 devices

Below is the code I am currently implementing:

view.loadUrl("javascript:var con = document.getElementsByTagName('img'); for (var i = 0; i < con.length; ++i)con[i].style.maxWidth='100%'; ");

When targeting API 18, this code functions properly. However, when targeting the latest Android version, API 19, it seems to remove the entire website content and replaces it with the text "100%". What could be causing this behavior specifically on Kit Kat?

Answer №1

There has been a significant update in how WebViews interact with javascript in KitKat. It is now recommended to avoid loading javascript using the loadUrl() method and instead utilize the new evaluateJavascript() method:

    view.evaluateJavascript(
            "var con = document.getElementsByTagName('img'); " +
            "for (var i = 0; i < con.length; ++i)con[i].style.maxWidth='100%';",
            null);

Answer №2

Upon realizing my previous suggestion was incorrect, I am clarifying it to prevent any confusion. Instead, I will provide some additional relevant information that may prove helpful in the future :-)

As Krylez pointed out, utilizing the evaluateJavaScript API can help avoid the aforementioned issue. It is worth noting that if you set your target SDK to be less than 19 and continue using the traditional loadUrl method with javascript: URLs, the WebView will automatically handle the fix for you. However, once you update your target to 19 or above, transitioning to evaluateJavaScript is recommended.

To better understand how the fix is implemented, you can refer to the following code snippet:

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

Issue: Java error - unrecognized flag: -release

I utilize Java : Java(TM) SE Runtime Environment (build 9-ea+143) Java HotSpot(TM) 64- Bit Server VM (build 9-ea+143, mixed mode) and IntelliJ IDEA: IntelliJ IDEA 2016.2.5 Build #IU-162.2228.15, built on October 14, 2016 Error encountered during compil ...

What is the best way to obtain output using a thread in a similar manner to

import java.util.Scanner; class CubaThread extends Thread { public CubaThread (String s) { super(s); } public void run() { int num; Scanner sc = new Scanner(System.in); System.out.println("Enter a positive number: "); num = Inte ...

What could be the reason my mat-form-field is not displaying the label?

I'm currently working on a project using Angular Material to build a web page. I am facing an issue with the mat-form-field component as the label is not displaying, and I can't figure out why. Any assistance would be greatly appreciated. Thank y ...

Struggling to send an array through Ajax to Django view?

I am just starting to work with ajax and I want to pass an array from JavaScript to my view. Here is the template. I have a form that takes the course ID number and score from students, creates a table, and adds how many courses the student wants to add. ...

How is it possible that the fourth element in the array displays a length of 24 when all it contains is the word "Spain"?

One challenge I'm facing is comparing the 5th element of my array with the value of the object that has been clicked. Below is the code snippet I am currently using: var options = new Array(); $(".option").click(function compareReaction(name) { ...

How can I extract and print only the desired div content and send it via email from an HTML/JS page?

Is there a way to print only specific content within a div, rather than the entire webpage in JS/HTML? Below is the code snippet I am using: function displayRadioValue() { let section1 = document.querySelectorAll('.section-1 > input[type="ra ...

Shortcut in JavaScript for verifying a specific key-value within an array of objects

As an angular developer, I have limited knowledge of JavaScript shorthand methods. I recently encountered a situation where I needed to loop through an array using a for loop in JavaScript. Let's say I have an array of Objects like: var arr = [{nam ...

What is the best way to integrate a Next.js Image component with a set width and an adaptable height in order to maintain the image's proportions?

This code snippet uses ChakraUI styling and retrieves images from SanityCMS: <Box w="500px" h="500px" bg="red"> <Image src={allArtPieces[0].imageUrl} alt={allArtPieces[0].title} width="500px" ...

Bring in various React components from separate files

I am struggling with using a component from another JS file in ReactJS. The error message "Header is not defined" keeps popping up. Just to clarify, I am working with ReactJS but without NodeJS involved. Here's the snippet of code causing the issue: ...

Understanding Variables in Javascript Tutorial

After diving into the tutorials here, I find myself struggling to grasp some elements of this example. Why is the variable initialized as an empty string and what does the ,i signify? var x="",i; Furthermore, why the need for x=x at the start of the l ...

Browsersync and Babel Express are set up to work together, with the Express server ready and waiting

Currently, I have set up a basic static server using Express in a separate server file. In my Gulpfile configuration, I am utilizing nodemon to run the server file and then passing its address to browsersync for proxying. Upon navigating to the webpage in ...

Exploring the world of three.js, webGL, and GLSL through the magic of random

When using three.js to call a fragment shader, I have a shader that specifies a color for my material in rgb format. I am trying to figure out a way to multiply those colors by a random value. The code I currently have is as follows: gl_FragColor = vec4( ...

Angular looping does not display data as expected

I am trying to display data from a JavaScript for loop inside a modal using ng-repeat for detailed information. Below is the snippet from my .js file: $http.get('.....').then(function(response){ $scope.coba = response.data; ...

Having trouble passing a JavaScript variable through AJAX requests

In this particular script, I am encountering an issue where the variable "encrypted" is expected to be sent via ajax to upload.php, however I am unable to achieve this. Oddly enough, if I substitute the "encrypted" variable with another variable in the a ...

Is there a smooth and effective method to implement a timeout restriction while loading a sluggish external file using JavaScript?

Incorporating content from an external PHP file on a different server using JavaScript can sometimes be problematic. There are instances where the other service may be slow to load or not load at all. Is there a method in JavaScript to attempt fetching th ...

Extract the value of an HTML tag and assign it to a variable

Can someone help me out with this, as I am not very familiar with Smarty: When I use ajax to submit this form: <form accept-charset="UTF-8" method="post" onSubmit="return validate()"> <input type="text" name="code" maxlength="15" class=" ...

How can I embed a Python processing sketch into a web page?

The question is simple. I dedicated a significant amount of time to creating a design in Processing using Python. Now, I am interested in displaying the sketch on a website. Naturally, one option would be to convert the Python code to JavaScript and uti ...

Identifying specific time intervals within an array of timestamps using jQuery

Currently, I am utilizing jQuery to manage a series of one-hour time intervals in an array. The array includes sets like: {hours[0] = '12a-1a', hours[1] = '1a-2a', hours[2] = '2a-3a', hours[3] = '2p-3p', hours[4 ...

Storing data in the browser's LocalStorage after a Vue3 component has

Whenever a user successfully logs into the application, a nav bar is supposed to load user data. However, there seems to be a timing issue with localStorage being set slightly after the nav bar is loaded. Using a setTimeout() function resolves the issue, b ...

jQuery GetJson fails to execute success function

I'm trying to use this code for an Ajax request to a JSON file: let formData = $("#myform").serialize(); $.getJSON("/files/data.json?" + formData, function(response){ alert(response); } ); However, there are no errors and the alert is n ...