Delphi's interaction with JavaScript

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.