简体   繁体   中英

how to access javascript custom global variable in another function

I have developing phonegap android application. I'm trying to establish the communication between javascript and android java classes.

For Communication i have used addJavaScriptInterfaceMethod in android main activity.So that i can use java object and method in javascript.

following is the Java Code :

 public class Activity extends DroidGap
  {
       @Override
       public void onCreate(Bundle savedInstanceState) 
       {

            super.onCreate(savedInstanceState);
            setIntegerProperty("loadUrlTimeoutValue", 60000);
            super.loadUrl("file:///android_asset/www/jq3.html");
            MapTablesWrapper table = new MapTablesWrapper();
            super.appView.addJavascriptInterface(new MapTablesWrapper(), "tables");

           }
     }

And in javascript :the following code does not printing the table value.It's alerting undefined

var tableName = tables.getTableName();
function(){
       alert(tableName);

 }

And if i declare var table inside the function.Its printing the tableName and following code is

 function(){
       var table = tables.getTableName();
       alert(tableName);
 }

But i would like to declare variable outside of the function and want to use that varaible in inside the function.

You can do like

var global = {
    table : null
};

function()
{
   global.table = tables.getTableName();
   alert(global.table);
}

you can rename global as your JS file name, so that this variable can be accessed all the functions in the current file and also in any other JS File by using your fileName.table here it is global.table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM