On the network also wrote about Delphi and JavaScript articles, most of the use of ScriptControl, etc., can not achieve with Delphi itself fusion effect. I also found this previously collected code while flipping through my library of components. This is mostly using Mozilla's JavaScript engine, so you have to bring the two dynamic link libraries of Js3215R.dll and MSVCR70.dll when the program is running.
Now let's look at a few examples:
Routine 1:
Description: The routine is mainly to put JavaScript code in the form of a string inside the Delphi program code, and then interact with Delphi.
Unit Form;
{$I Delphi.inc}
Interface
Uses
Windows, Messages, Sysutils, {$IFDEF d6or7}variants, {$ENDIF}classes, Graphics, Controls, Forms,
Dialogs, Stdctrls, jsintf;
Type
TForm1 = Class (Tform)
Button1:tbutton;
Edit1:tedit;
Procedure Button1Click (Sender:tobject);
Procedure Formdestroy (Sender:tobject);
Procedure Formcreate (Sender:tobject);
Private
Fengine:tjsengine;
Public
End
Var
Form1:tform1;
Implementation
{$R *.DFM}
Procedure Tform1.formcreate (Sender:tobject);
Begin
Fengine: = Tjsengine.create (40000);
Fengine.startdebugger;
FEngine.Global.AddNativeObject (Edit1, ' edit ');
FEngine.Global.AddNativeObject (Button1, ' button ');
FEngine.Global.Evaluate (' function toggle () {' +
' Edit. Visible =! Edit. visible; ' +//Toggle the Visible property on/off
' button. Caption = (edit. Visible? "Hide": "Show"); ' +//Change button
‘}‘);
End
Procedure Tform1.formdestroy (Sender:tobject);
Begin
Fengine.free;
End
Procedure Tform1.button1click (Sender:tobject);
Begin
FEngine.Global.Evaluate (' Toggle () ');
End
End.
------------------------------------------------------------------------------------------------------
Routine 2:
Note: The JavaScript code is placed outside the Delphi program on the basis of the routine.
Unit Form;
{$I Delphi.inc}
Interface
Uses
Windows, Messages, Sysutils, {$IFDEF d6or7}variants, {$ENDIF}classes, Graphics, Controls, Forms,
Dialogs, Stdctrls, jsintf;
Type
TForm1 = Class (Tform)
Button1:tbutton;
Edit1:tedit;
Procedure Formcreate (Sender:tobject);
Procedure Formdestroy (Sender:tobject);
Procedure Button1Click (Sender:tobject);
Private
Fengine:tjsengine;
Fscript:tjsscript;
Public
End
Var
Form1:tform1;
Implementation
{$R *.DFM}
Procedure Tform1.formcreate (Sender:tobject);
Begin
Fengine: = Tjsengine.create (40000);
Fscript: = tjsscript.create;
FEngine.Global.AddNativeObject (Edit1, ' edit ');
FEngine.Global.AddNativeObject (Button1, ' button ');
Fscript.loadraw (' script.js '); Load the JavaScript code from file
Fscript.execute (Fengine); Register the function with the JS engine
End
Procedure Tform1.formdestroy (Sender:tobject);
Begin
Fengine.free;
Fscript.free;
End
Procedure Tform1.button1click (Sender:tobject);
Begin
FEngine.Global.Evaluate (' Toggle () ');
End
End.
The JavaScript code is as follows:
function Toggle () {
Edit. Visible =! Edit. Visible; Toggle the Visible property on/off
button. Caption = (edit. Visible? "Hide": "Show"); Change button
}
There was still a routine I would not post, and finally all the code package to provide download, I wish you enjoy youself!
Click here to download this file (2.07 MB)
Http://www.lsworks.net/article/56.html
Delphi's interaction with JavaScript