Coolguy
Coolguy

Reputation: 2285

Using EvaluateScriptAsync to call javascript in CefSharp

Currently using CefSharp in my Visual Studio project to show a web browser. I'm using the EvaluateScriptAsync to call a function in my javascript.

But I encounter a small problem.

The below can work:

    string strMsg = "12345";
    var script = string.Format("testing({0});", strMsg);
    browser.EvaluateScriptAsync(script);

The below cannot work:

    string strMsg = "ABCDE";
    var script = string.Format("testing({0});", strMsg);
    browser.EvaluateScriptAsync(script);

Then on the html side:

  testing = function (error) {
      alert(error);
      return false;
  };

Why is it I can't send alphabets string?

Is there a difference between sending a numbers and sending alphabets?

Upvotes: 1

Views: 2612

Answers (1)

amaitland
amaitland

Reputation: 4420

The string you generate will execute as JavaScript. As it stands in your second example when the code executes it would be looking for a variable named ABCDE. You need to encapsulate in quotes to make it into a string.

string strMsg = "ABCDE";

should be

string strMsg = "`ABCDE`";

or

string strMsg = "\"ABCDE\"";

For debugging, CefSharp supports DevTools which you can open and see JavaScript console output.

Upvotes: 1

Related Questions