Android Question Javascript in webview issue

Alessandra Pellegri

Active Member
Licensed User
Longtime User
I have this simple javascript code:
B4X:
function ColoraSolo(elemento,colore) {
   var a = document.getElementById("svgObject");
   var svgDoc = a.contentDocument;   
   var baz = getElementsByAttribute(svgDoc,"clrto","xxy1");
   for (i = 0; i < baz.length; i++) {
     var att = document.createAttribute("opacity");
     att.value = 0;
     baz[i].setAttributeNode(att);
   }
   var svgItem = svgDoc.getElementById(elemento);
var child = svgItem.children;
var att = document.createAttribute("opacity");
att.value = 0.4;
var attw = document.createAttribute("fill");
attw.value = colore;
var ricn = document.createAttribute("clrto");
ricn.value = "xxy1";
for (i = 0; i < child.length; i++) {           //<<<------ Line 35
   child[i].setAttributeNode(att);   
   child[i].setAttributeNode(attw);
   child[i].setAttributeNode(ricn);   
}
}

When I launch it from WebViewExtras.executeJavascript I have this error:
B4X:
Uncaught TypeError: Cannot read property 'length' of undefined in file:/// (Line: 35)
Using it from pc it works good.

If I put an 'alert(svgItem)' before the error I obtain: [object SVGElement]
But if I put 'alert(child)' the result is : undefined

It is very strange because on the PC the code works perfectly. Is it a bug of webview ?

Thank you
 

Roycefer

Well-Known Member
Licensed User
Longtime User
I can't specifically say what the problem is, your code looks correct. But I can say that different browsers/webviews have different JavaScript engines. It will ease your trouble-shooting if your PC browser is using the same JavaScript engine as your Android webview. To that end, I'd recommend using Chrome or Opera on the PC to run the code and make sure you add a WebChromeClient to your webview.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
What kind of object is svgItem? If it is SVG then take a look here http://caniuse.com/#search=svg , It reports that Android 4.1 - 4.3 have only partial support, could that be the problem. What version of Android are you running?
 
Upvote 0
Top