C # using the technique of invoking JS script

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.