call pascal function from javascript

hi,

how i can call pascal function from javascript code ?

This is an example:



// this is a Pascal method
procedure MyPascalProc(s: string);
begin
  ShowMessage(s);
end;

procedure TForm1.WebButton1Click(Sender: TObject);
begin
  asm
    // this is pure Javascript code
    $impl.MyPascalProc("test");
  end;
end;

dont work
i missed Something ?

delphi 10.2.3
webcore 1.0.1.0

Please specify "don't work".
Do you see an error? What error? Did you add this code to a form unit?

the click on WebButton1 dont show the message.




See test project: http://www.tmssoftware.net/public/jscallproc.zip
Bruno Fierens2018-08-09 12:25:02

your zip work fine.
thanks for help

PS :
if i declare the procedure in unit before implementation ( that was my case), stop working.

If it is in the interface section, you can use:


procedure TForm2.WebButton1Click(Sender: TObject);
begin
  asm
    // this is pure Javascript code
    $mod.MyPascalProc("test");
  end;
end;
when i call pascal function from javascript like that

form1.webhtmlcontainer.HTML.Text :=
   '<div onclick="pas.unit1.test(''Hello'')"> Click me show message </div> '

unit1 contain :
function test(message:string):boolean; 
begin showmessage(message); end;




in debug mode function test is  called
in relase mode, function test is not called, why ??

any help ?



solved when :
project->options->TMS Web->opimisation->false

The compiler cannot know that  some Pascal function is accessed from JavaScript code, hence it can strip this code when optimization is chosen.

Hello,
I know this discussion is quite old, but i just need a complementary piece of information on this specific case :
Is there a way to let the optimization active and tell the optimizer to avoid interfering with a specific function, for example with {$ } compilation instructions around the function implementation ?

Sorry, there is at this moment not a compiler define for this.
Used code should not be optimized away, only unused code.

i resolved this problem like this way :

1- TFEBack = Function (qelt: tjselement) : boolean;
2- e_onclickReturn : TFEBack;
3- qtd := document.getElementById('elt_id') ; // qtd is a tjselement
4- qtd.addEventListener('click', @self.handleevent_a); // click or what you want

5-
function handleevent_a(Event: TJSMouseEvent): Boolean;
begin
qelt := tjselement(event.target) ;
elistClick (qelt);
end;

6-
Function elistClick (qelt: tjselement) : boolean;
begin
showmessage(qelt.innerHTML);
end;