1, create a WinForm project. 2, add a text box to a button on the From1. 3, create a test.js file in the solution. The Test.js code is as follows: function SayHello (str) {return "Hello,"+str;}4, enter the Form1.cs code backstage. Private voidButton1_Click (Objectsender, EventArgs e) { stringPath = AppDomain.CurrentDomain.BaseDirectory +"Test.js"; stringSTR2 =file.readalltext (path); stringFun =string. Format (@"SayHello (' {0} ')", This. TextBox1.Text.Trim ()); stringresult =Executescript (fun, str2); MessageBox.Show (result); } /// <summary> ///Execute JS/// </summary> /// <param name= "Sexpression" >parameter Body</param> /// <param name= "SCode" >A string of JavaScript code</param> /// <returns></returns> Private stringExecutescript (stringSexpression,stringSCode) {Msscriptcontrol.scriptcontrol ScriptControl=NewMsscriptcontrol.scriptcontrol (); Scriptcontrol.usesafesubset=true; Scriptcontrol.language="JScript"; Scriptcontrol.addcode (SCode); Try { stringstr =Scriptcontrol.eval (sexpression). ToString (); returnstr; } Catch(Exception ex) {stringstr =Ex. Message; } return NULL; }
Reference: http://www.cnblogs.com/xdpxyxy/archive/2013/06/11/3132097.html
C # Use Tips for invoking JS scripts (GO)