简体   繁体   English

使用EvaluateScriptAsync在CefSharp中调用JavaScript

[英]Using EvaluateScriptAsync to call javascript in CefSharp

Currently using CefSharp in my Visual Studio project to show a web browser. 当前在我的Visual Studio项目中使用CefSharp来显示Web浏览器。 I'm using the EvaluateScriptAsync to call a function in my javascript. 我正在使用EvaluateScriptAsync在我的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: 然后在html端:

  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? 发送数字和发送字母之间有区别吗?

The string you generate will execute as JavaScript. 您生成的字符串将作为JavaScript执行。 As it stands in your second example when the code executes it would be looking for a variable named ABCDE . 正如您在第二个示例中所说的那样,在代码执行时,它将寻找一个名为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. 对于调试, CefSharp支持DevTools ,您可以打开并查看JavaScript控制台输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Cefsharp EvaluateScriptAsync 获取返回值 - How to get return value from a Cefsharp EvaluateScriptAsync 为什么 EvaluateScriptAsync 在 CefSharp 中返回一个空的 expando 对象 - Why is EvaluateScriptAsync returning an empty expando object in CefSharp 从CefSharp 1中的javascript调用.Net - wpf - Call .Net from javascript in CefSharp 1 - wpf 在 Windows 窗体应用程序中使用 CefSharp 从 javaScript 调用 C# 函数 - Call C# function from javaScript using CefSharp in Windows Form App 使用CefSharp和SchemeHandler如何创建Javascript File() - Using CefSharp and a SchemeHandler how to create a Javascript File() 使用带有CefSharp或CefGlue的JavaScript的本地或流式音频 - Local or streamed audio using JavaScript with CefSharp or CefGlue 如何将 VB.net 代码中定义的字符串变量的值传递到 CefSharp 浏览器中,并使用 Javascript 按名称调用它们? - How can I pass the values of string variables defined in VB.net code into a CefSharp browser and call them by their name using Javascript? 如何通过 EvaluateScriptAsync 访问 javascript 中的 c# 变量? - how to access c# variable inside javascript through EvaluateScriptAsync? 使用CefSharp.Offscreen检索需要Javascript渲染的网页 - Using CefSharp.Offscreen to retrieve a web page that requires Javascript to render 使用 javascript 从 CefSharp 浏览器获取文本框值 - Getting textbox values from a CefSharp browser using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM