Ugur Ugur - 20 days ago 10
Javascript Question

App Inventor 2 Webviewstring to Javascript: Defining an array

OK. What I'm trying to do is to pass an array via the webviewstring. for this, I use blocks to join list items into this: ["item1", "item2", "item3"]. I pass it through, and define this:

var itemlist = window.AppInventor.getWebViewString();


but when I try to call item 0 in the array, it gives me this:
[
and as item 1, it gives
"
, and item 2, it gives
i
.

If I use

document.getElementById("list0").innerHTML = itemlist;


it returns
["item1", "item2", "item3"];


What I believe happens is that it puts the whole webviewstring inside brackets like this: (
["item1", "item2", "item3"];
), so when I call itemlist, it also returns the
;
at the end.

Can anyone tell me how I can translate my webviewstring into an array?

Answer

my suggestion is to pass the data in csv format from App Inventor to the HTML file

enter image description here

and then convert it into an array in the HTML file like this

 // get the table to display from the window.AppInventor object and split at new line
 var urlArray = window.AppInventor.getWebViewString().split("\n");

see also a complete example here