Recent

Author Topic: How to call external JS script?  (Read 4456 times)

zendrael

  • New Member
  • *
  • Posts: 15
How to call external JS script?
« on: October 16, 2023, 02:43:45 pm »
Hello!

I need to call an external JS script, but I don't want to declare all of the external script functions and don't know all of them...

Can I do something like
Code: Pascal  [Select][+][-]
  1. window.ExternalLibName.function()
inside my Pascal code?

Thanks in advance, any help is appreciated.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5759
  • Compiler Developer
Re: How to call external JS script?
« Reply #1 on: October 16, 2023, 10:26:40 pm »
I need to call an external JS script, but I don't want to declare all of the external script functions and don't know all of them...

The point of Pascal is to declare first. So you must to declare them.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11946
  • FPC developer.
Re: How to call external JS script?
« Reply #2 on: October 17, 2023, 11:55:29 am »
Does pas2js support idispatch ? :-)

PascalDragon

  • Hero Member
  • *****
  • Posts: 5759
  • Compiler Developer
Re: How to call external JS script?
« Reply #3 on: October 18, 2023, 10:02:43 pm »
Does pas2js support idispatch ? :-)

From what I can see it doesn't.

ps

  • Full Member
  • ***
  • Posts: 136
    • CSS
Re: How to call external JS script?
« Reply #4 on: November 08, 2023, 07:09:07 am »
I need to call an external JS script, but I don't want to declare all of the external script functions and don't know all of them...

You can declare only function you need. For example:

Code: Pascal  [Select][+][-]
  1.   TJSSignaturePad = class external name 'SignaturePad' (TJSObject)
  2.  
  3.     constructor new (aTarget : TJSElement; const aOptions : TJSObject);
  4.     procedure fromData(Value: JSValue);
  5.   end;    
Small simple CSS/box model implementation: https://github.com/pst2d/csscontrols/tree/dev

d-_-b

  • New Member
  • *
  • Posts: 48
Re: How to call external JS script?
« Reply #5 on: February 21, 2024, 10:11:10 pm »
Why not use a asm block?
But read "Why are asm blocks bad".

Below is a fully working pas2js example, note Result is case-sensitive in the asm block!
Code: Pascal  [Select][+][-]
  1. var
  2.   val: jsvalue;
  3. function movementQueryWorld: jsvalue;
  4. begin
  5.   asm
  6.      Result = window.movementQuery(world) // https://github.com/NateTheGreatt/bitECS/blob/master/docs/INTRO.md#-query
  7.   end;
  8. end;
  9. begin
  10.   val:= movementQueryWorld();
  11.   writeln('From Pascal', val);
  12. end.
  13.  
Code: Pascal  [Select][+][-]
  1. mov     ax,0013h
  2. int     10h
Denthor thanks for the vga programming tutorials | Download all tutorials

zendrael

  • New Member
  • *
  • Posts: 15
[SOLVED] Re: How to call external JS script?
« Reply #6 on: March 04, 2024, 12:45:26 pm »
Thank you all!

Solved by creating a wrapper in JS and on Pascal with calls for external wrapper functions. This way I can change whatever is needed in the wrapper and keep the calls in Pascal.

Thank you for all the help!

 

TinyPortal © 2005-2018