Passing parameters from HTML to Droidscript

58 views
Skip to first unread message

Syed Munawer Hassan

unread,
Jan 28, 2023, 12:53:50 PM1/28/23
to DroidScript
Hi
How to pass array from html to Droidscript

<html>
<script src="Myapp.js"></script>
<script> arr=[1,2,3,4,5,6,7,8,9,10]</script>
</html>

DS App (Myapp.js)

for (a=0;a<10;a++){
app.Alert(a);
}



Right2TheV0id

unread,
Jan 28, 2023, 2:06:20 PM1/28/23
to DroidScript
On a regular DroidScript HTML App it would work easily:

<html>
<head>
    <meta name="viewport" content="width=device-width">
    <script src='file:///android_asset/app.js'></script>
</head>
<script> arr=[1,2,3,4,5,6,7,8,9,10]</script>
<script>
    function Start()
    {

        for (a=0;a<10;a++){
            app.Alert(a);
        }
    }
</script>
<body onload="Start()">
</body>
</html>

But you seem to try to do something different, could you share an spk?

Alan Hendry

unread,
Jan 28, 2023, 2:51:21 PM1/28/23
to DroidScript
Hi,
You could try converting to a string (tostring, see also JSON tostring for objects)
Send from html to DS, then split back to an array
Regards, ah

Syed Munawer Hassan

unread,
Jan 29, 2023, 9:53:05 PM1/29/23
to DroidScript
what I need is to use global array which can be used by both html and DS

Jumar

unread,
Jan 30, 2023, 1:38:32 AM1/30/23
to DroidScript
Hi

I don't really understand what you mean by "How to pass array from html to Droidscript" nor "Passing parameters from HTML to Droidscript".

I think what you have is this scenario

1. You have a native DroidScript app which is your main app.
2. In your main app, you have a webview which is your html app which we know that javascript in DS WebView runs in another thread.
3. You need to pass array from your main app to your html in the webview.

If I get it right, then here's the short code snippet for you.

In your mainApp.js file
function OnStart() {
    lay = app.CreateLayout( "linear", "VCenter,FillXY" )    
   
    web = app.AddWebView(lay, 0.9, 0.8);
    web.SetOnProgress(OnProgress);
    web.LoadUrl("myWeb.html");

    app.AddLayout( lay )
}
function OnProgress(v) {
    // make sure the html is fully loaded
    if(v == 100) {
       
var arrData = [1, "Some text", true];
        web.Func("myHtmlFunction", arrData);
    }
}

In your myWeb.html file
<html>
    <head>

        <script src='file:///android_asset/app.js'></script>
        <title>Hello World!</title>
        <script>
           
function myHtmlFunction(arrData) {
                console.log(arrData);
            }

        </script>
    </head>
    <body>
        <p>Hello World!</p>
    </body>
</html>

Alternatively, if you want to pass data from the webView html to the main app, here is the code snippet

In your mainApp.js file
function OnStart() {
    lay = app.CreateLayout( "linear", "VCenter,FillXY" )    
   
    web = app.AddWebView(lay, 0.9, 0.8);
    web.LoadUrl("myHtml.html");

    app.AddLayout( lay )
}
function myMainFunc(arrData) {
    console.log(arrData);
}


In your myWeb.html file
<html>
    <head>

        <script src='file:///android_asset/app.js'></script>
        <title>Hello World!</title>
        <script>
            function OnStart() {
               
var arrData = [1, "Some text", true];
                app.Func("myMainFunc", arrData);  
 
            }
        </script>
    </head>
    <body onload="app.Start()">
        <p>Hello World!</p>
    </body>
</html>

Regards
Jumar

Alan Hendry

unread,
Jan 31, 2023, 2:13:27 PM1/31/23
to DroidScript
Nice,
This also shows how to use web.Func for DS to invoke function in webview,
and conversely app.Func for webview to invoke a function in DS.
Regards, ah
Reply all
Reply to author
Forward
0 new messages