Memo1里扔啥代码能正常运行?
unit Unit3;interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls;
type
TForm3 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
m_OLE: Variant;
{ Public declarations }
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
uses
ComObj;
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 TForm3.Button1Click(Sender: TObject);
var
OpenDlg: TOpenDialog;
FileName: string;
begin
OpenDlg := TOpenDialog.Create(nil);
try
OpenDlg.Filter := 'JavaScript脚本|*.*';
OpenDlg.DefaultExt := '*.*';
if OpenDlg.Execute then
begin
FileName := Trim(OpenDlg.FileName);
if FileName <> '' then
begin
Memo1.Lines.LoadFromFile(FileName, TEncoding.UTF8);
end;
end;
finally
FreeAndNil(OpenDlg);
end;
end;
procedure TForm3.Button2Click(Sender: TObject);
var
s: string;
begin
s := RunJs(Memo1.Text, 'str');
ShowMessage(s);
end;
procedure TForm3.Button3Click(Sender: TObject);
var
js: OleVariant;
begin
js := CreateOleObject('ScriptControl'); //创建组件
js.Language := 'JavaScript'; //指定组件所使用的语言,也可以是VBScript
ShowMessage(js.Eval('100+1')); //计算100+1的值
js := Unassigned;
end;
end.
页:
[1]