要引用的DLL
Interop.MSScriptControl.dll
winform执行js方法
private void button1_Click(object sender, EventArgs e)
{
//js文件
string path = AppDomain.CurrentDomain.BaseDirectory + "test.js";
string str2 = File.ReadAllText(path);
// 要执行的js方法名(参数)
string fun = string.Format(@"sayHello('{0}')", this.textBox1.Text.Trim());
string result = ExecuteScript(fun, str2);
MessageBox.Show(result);
}
 
/// <summary>
/// 执行JS
/// </summary>
/// <param name="sExpression">参数体</param>
/// <param name="sCode">JavaScript代码的字符串</param>
/// <returns></returns>
private string ExecuteScript(string sExpression, string sCode)
{
MSScriptControl.ScriptControl scriptControl = new MSScriptControl.ScriptControl();
scriptControl.UseSafeSubset = true;
scriptControl.Language = "JScript";
scriptControl.AddCode(sCode);
try
{
string str = scriptControl.Eval(sExpression).ToString();
return str;
}
catch (Exception ex)
{
string str = ex.Message;
}
return null;
}

相关文章:

  • 2022-12-23
  • 2021-08-07
  • 2022-12-23
  • 2022-12-23
  • 2021-11-14
  • 2022-12-23
  • 2021-11-14
  • 2021-07-10
猜你喜欢
  • 2022-12-23
  • 2021-09-28
  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案