简体   繁体   中英

Cannot get return value from Javascript

I already succeed using this .

But in this case I need pass the parameter and get return value in one time from javascript using this method. But this code is not working:

int idx=1;
chromeBrowser.EvaluateScriptAsync(
   string.Format(@"
    var i = {0};
    return i;
", idx)
).ContinueWith(x =>
{
   var response = x.Result;

   if (response.Success && response.Result != null)
   {
       this.Invoke((MethodInvoker)delegate
       {
           int atemp = (int)response.Result;
           MessageBox.Show(atemp.ToString());
       });
   }
});

Please help me to solve this problem.

I believe the problem is that your JavaScript code should be a self invoking function .

Try rewriting the function to something like this:

(function() {
  var i = 1;
  return i;
})()

 console.log((function() { var i = 1; return i; })()); 

As you can see from the snippet above, this code actually returns a value. You can use this JavaScript in your C# like this:

int idx = 1;
chromeBrowser.EvaluateScriptAsync(
    string.Format(@"
        (function() {
          var i = {0};
          return i;
        })()
    ", idx)
).ContinueWith(x =>
...

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