ver at stackoverflow someone asked, "Is it possible to utilize javascript in the making of windows desktop applications". Why yes, not only is it possible, but it's actually quite powerful. In this brief article I describe how you can add script support to your Windows desktop projects.procedure TSomeForm.FormCreate(Sender: TObject); begin // Create our javascript engine JavaScript := TJavaScript.Create(Self); // Make this form available to javascript under the name 'Form' FScript.AddScriptlet(CreateComponentScriptlet(Self, 'Form')); // Add hosting support including our Alert and Echo methods FScript.AddScriptlet(CreateHostScriptlet(ScriptAlert, ScriptEcho)); end;
procedure TSomeForm.ExecuteButtonClick(Sender: TObject); begin JavaScript.Lines := SourceMemo.Lines; JavaScript.Execute; end;
/* Example hosted javascript */
/* See ScriptIntf.pas for THostScriptlet's methods */
Host.Echo("Hello from javascript");
/* See ScriptIntf.pas for TComponentScriptlet's properties */
Host.Alert("Save button anchors are: " + Form.SaveButton.Anchors);
Form.ExecuteButton.Caption = "Hello";
/* Example hosted javascript */
var a = 12;
function inverse(value) {
return 1 / value;
}procedure TSomeForm.Test;
var
Script, A: OleVariant;
begin
if JavaScript.State = ssConnected then
begin
Script := JavaScript.Script;
A := Script.a;
ShowMessage(A.toString().length);
A := 0.25;
ShowMessage(Script.inverse(Script.a));
ShowMessage(Script.inverse(0.25));
end;
end;