IMPORTANT : HowTo access javascript variable from pascal

Is it possible to access from pascal code (function) of a global javascript variable declared from a js file

Myfile.js (declared in Myprogram.html like jquery and other one)
var MyJsVar;

and use or reach from pascal function passed as argument

(pascal code)
procedure myform.myfunction(MyJsVar : Idon't_Know_What_Type_Is);
begin
asm
MyJsVar.DoSomething;
end;
end;

Thanks at all.

I found the following informations to be useful for me. Your "Idon't_Know_What_Type_Is" type will most likely be the "JSValue" data type. Using ASM, you can access any JavaScript variable. Use the F12 devtools console to play the necessary keyword bingo to get hold of any JS variable you are looking for.

Background information:
   - The type JSValue represents a "native" javascript variable. It can be
     best compared with the Delphi data type "Variant". A variable of type
     JSValue can have any of the data types listed in TJSValueType of unit JS.
     This also includes TJSObject and TJSArray;

   - The actual data type of a JSValue can be checked using GetValueType() or
     the many "isXYZ()" functions in unit JS.

   - A JSValue can be assigned any allowed Delphi data type without any conversions
       Var J : JSValue; J := 'Hey'; J := 1.14
     To copy the content of a JSValue into a Delphi variable, simple conversions
     have to be made
       Var S : String; S := String(J)
       Var D : Double; D := Double(J)

   - The class TJSObject is an associative array of key-value pairs, where the
     values are of type JSValue. Nesting of objects is possible and this is
     what makes up the JSON analogy. Import and export from/to a JSON string
     is already natively possible. No need to write extra code for this!

   - The class TJSArray is a simple "Array of JSValue"

   - A JSValue has no constructor. TJSObject and TJSArray have the constructor
     "new" (not "Create" as normally in Delphi). As javascript offers garbage
     collection, there is no need to "Free" any of these objects.

Thanks Walter
In fact this not really I'm searching for.
Because of non persistence of TMS Web Core view concept, I must keep all views data through global variables.
To accomplish that I use JS global variables declared in a main js file
loaded from my appli (from start)

I have in a main js file variables declared like this :

var aJS_tabulator;
var aJS_dataset;

I just want to load them from a pascal funct/proc.

procedure MyProc()
begin
aJS_tabulator;

// something like getElementByID() but getVariableByName

end;

I ask me if it's possible ????

1 Like

You should be able to access such global JavaScript variables from ASM blocks. Assign these from ASM blocks to Pascal variables if you want to use these from Pascal code.

Hi Mr Fierens,

1/

I need to keep a TABULATOR table content (from tabulator.io) during navigation of my appli.

To have this feature I use some global Javascript variable declared in my main.js file as => var MyJSTab ;

what kind of Pascal variable type should I use in my pascal program. to replace or to link MyJSTab with aPascalJSVar. (JSElement, JSObject ???) the doc is not clear about it.

2/

So to my last mail, you said that form.components is ok, but I can navigate to

component declared in my form.

I would just like assigned all my TwebEdit component named like this SF_xyz to something

I use pascal simple plan :

var

I,nb : integer;

acomp : Tcomponent;

nb := form.components.count;

for i:=0 To nb-1 do

begin

acomp := Form.components[i];

If (pos (‘SF_’,acomp.name) > 0 then DoSomething.

end;

I my form I have 5 TwebEdit component but nb=1 and the only one detected is a THttpRequest

Thank you

I guess your form is not owner in this case, just parent.
So, try instead
for i := 0 to form.ControlCount - 1 do
with form.Controls[i] do ....

Hi Bruno,

it doesn’t work.

I have a form with a clear button and some TWebEdit in a form (mapped on htmlform element)

I have this function :

{-----------------------------------------------------------------------------

Procedure: W_CLEARClick

-----------------------------------------------------------------------------}

procedure TGrpForm.W_CLEARClick(Sender: TObject);

var

i,

nb : integer;

aComp : TComponent;

begin

nb := Self.ComponentCount;

for i:=0 TO nb-1 do

begin

aComp := Self.Components[i];

ShowMessage(aComp.Name);

end; // THIS DOESN’T WORK

// SF CLEARING (I have 12 TWebEdit in this case)

for i := 0 to ControlCount - 1 do

if (pos('SF_',UpperCase(Controls[i].Name)) > 0)

then TWebEdit(Controls[i]).Text := ''; // THIS IS FORBIDDEN

//WAITER

W_DIVWAITER.Visible := false;

// RESULT

W_RESULT.Visible := false;

end;

How can I reach some properties of my TWebEdit components inside a loop ?

I don’t want use html feature as « reset ».

As the forms are not persistent, I have to use some tricks to keep the data between

them and when I browse my app. Muy method is to record some couple of data in global Tstringlist

before quit a form to reassign when I return on it. I can use each componeent one by one and

assign or keep each value. And using their name and a loop it makes sense to me.

IMPORTANT !!! In my last mail I ask you in which datatype must I have declare in pascal code to keep

a javascript variable declared from my main js file.

I have var Jsvar; and I want mapped it with pascal var.

At my opinion it would be very cool to have some structure as delphi

to navigate inside it and to have a form persistence feature without

being obliged each time to recreate the form

Thank you very much.

Sorry for my last mail this part is ok now Control instead Component runs good.

Thanks to confirm!