Rajesh
Rajesh

Reputation: 531

How to call more than one java script function at a time in android to load the webview?

i have a simple java script file.below is my sample js code

function autoplay(videoId, st, en) { player.loadVideoById({ 'videoId': videoId, 'startSeconds': st, 'endSeconds': en, 'suggestedQuality': 'small' }); player.playVideo(); }

    function getState(){
      alert(player.getPlayerState());
        return player.getPlayerState();
    }

    function CallTweak()
    {
      alert(player.getPlayerState());
      document.getElementById("mytext").innerHTML = player.getPlayerState();
      var data=player.getPlayerState();
       AndroidFunction.showToast(data);
      alert('second alert');
    }

i calling one function it works fine but i am trying to call two functions for example autoplay() and CallTweak() those methods are not called. below is sample code for calling the functions

web_view.loadUrl("javascript:CallTweak()"); web_view.loadUrl("javascript:autoplay()");

How to call more than one function at a time in a single button click in android?Thanks in advance.

Upvotes: 1

Views: 1019

Answers (1)

AmmarCSE
AmmarCSE

Reputation: 30587

Take one of two approaches

Either, in your javascript code, define a third function which calls the other two

function callBothFunctions(){
    CallTweak();
    autoplay();
}

and then call that third function

web_view.loadUrl("javascript:callBothFunctions()");

Or, the other approach is to use an anonymous self-executing function like

 string javascriptFunctionCall = 
                      "javascript:"
                      + "(function(){"
                          + "CallTweak();"
                          + "autoplay();"
                      +"})()";

 web_view.loadUrl(javascriptFunctionCall);

Upvotes: 1

Related Questions