本例效果图:
代码文件:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; Edit1: TEdit; Label1: TLabel; procedure Button1Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} uses ComObj; {此函数需要 ComObj 单元的支持} {参数 JsCode 是要执行的 Js 代码; 参数 JsVar 是要返回的变量} function RunJs(const JsCode, JsVar: string): string; var script: OleVariant; begin try script := CreateOleObject('ScriptControl'); script.Language := 'JavaScript'; script.ExecuteStatement(JsCode); Result := script.Eval(JsVar); except Result := ''; end; end; procedure TForm1.Button1Click(Sender: TObject); var s: string; begin s := RunJs(Memo1.Text, 'str'); ShowMessage(s); end; end.
转载于:https://blog.51cto.com/rosehacker/450215