rizkidzulkarnain
rizkidzulkarnain

Reputation: 61

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.

Upvotes: 1

Views: 3189

Answers (1)

Martin Benninger
Martin Benninger

Reputation: 597

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 =>
...

Upvotes: 4

Related Questions