delphi执行js

delphi执行js_第1张图片


命名空间:ComObj


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,ComObj;

type
  TForm1 = class(TForm)
    btn1: TButton;
    mmo1: TMemo;
    lbl1: TLabel;
    edt1: TEdit;
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    function ExecJs(Code,Func:string):string;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function  TForm1.ExecJs(Code,Func:string):string;
var script:OleVariant;
begin
  script:=CreateOleObject('ScriptControl');
  script.Language:='JavaScript';
  script.ExecuteStatement(Code);
  Result:=script.Eval(Func);
end;

procedure TForm1.btn1Click(Sender: TObject);
var ss:string;
begin
  ss:=ExecJs(mmo1.Text,Trim(StringReplace(edt1.Text,'()','',[]))+'()');
  ShowMessage(ss);
end;

end.


你可能感兴趣的:(delphi执行js)