DelphiでJSファイルを呼び出す方法

4608 ワード

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    m_OLE : Variant;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  tfname:string;
  StrList: TStringList;
begin
   // JS 
   m_OLE := CreateOleObject('ScriptControl');
   m_OLE.Language:='JavaScript';

   StrList:= Tstringlist.Create;
   try
     // JS 
     StrList.LoadFromFile(ExtractFilePath(ParamStr(0)) + 'Date.js');
     //  m_OLE  
     m_OLE.AddCode(strlist.Text);
   finally
     StrList.Free;
   end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  DateStr : string;
begin
  CoInitialize(nil);      // Variant    CoInitialize,CoUnInitialize
  DateStr := m_OLE.Eval('Now()');  //  JS now()  
  Memo1.Lines.Add(DateStr);
 
  DateStr := m_OLE.Eval('DateFormat("YEAR")');  // YEAR   JS DateFormat()  
  Memo1.Lines.Add(DateStr);
  CoUnInitialize;
end;


end.