C# 中直接调用js方法
请注意,我这段代码中实现js方法中有 funcName(argument) 这种一个入参的调用。
using System; namespace game2_36.Common { public class JSHELP { //string _MD5_js = "MD5.js"; private object ExecuteScript(string funcName, string argument, string jsPath) { string js = System.IO.File.ReadAllText(jsPath); object o = ExecuteScript(string.Format("{0}('{1}')", funcName, argument), js); return o; } /// <summary> /// 执行JS /// </summary> /// <param name="sExpression">参数体</param> /// <param name="sCode">JavaScript代码的字符串</param> /// <returns></returns> private object ExecuteScript(string sExpression, string sCode) { MSScriptControl.ScriptControl scriptControl = new MSScriptControl.ScriptControl(); scriptControl.UseSafeSubset = true; scriptControl.Language = "JScript"; scriptControl.AddCode(sCode); try { return scriptControl.Eval(sExpression); } catch (Exception ex) { } return null; } } }