Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / pas2js / 2019-09

Call a function from html

8 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Gabriele Cappelletto

Hello
I would like to find a way to call a function written with pas2js from html code (do not define onclick from pas2js, but call from html)

example
html
?<input type="button" class="form-control" id="btn3" value="direct mode" onclick="Btn1Click();"/>

and the Btn1Click function write it with pas2js.
example
function Btn1Click(aEvent: TJSMouseEvent): boolean;
begin
? //?
end;

How can I do?

Reply by email

Mattias Gaertner
to Gabriele Cappelletto
Show quoted text 17 lines
Hello I would like to find a way to call a function written with pas2js from html code (do not define onclick from pas2js, but call from html) example html ?<input type="button" class="form-control" id="btn3" value="direct mode" onclick="Btn1Click();"/> and the Btn1Click function write it with pas2js. example function Btn1Click(aEvent: TJSMouseEvent): boolean; begin ? //? end; How can I do?

For example:

var Btn3: TJSHTMLButtonElement;
Btn3:=TJSHTMLButtonElement(document.getElementById('btn3'));
;

Mattias

Reply by email

Michael Van Canneyt
to Gabriele Cappelletto: Hello I would like to find a way to call a function written with pas2js from html code…
Gabriele Cappelletto wrote 8 lines
Hello I would like to find a way to call a function written with pas2js from html code (do not define onclick from pas2js, but call from html) example html ?<input type="button" class="form-control" id="btn3" value="direct mode" onclick="Btn1Click();"/>

All functions live under the top-level "pas" object, per unit.

So

onclick=pas.YourUnit.Yourfunction()

should do it.

Here YourUnit and YourFunction are of course case sensitive.

If the function is in the main program, then you must write

onclick=pas.program.Yourfunction()

with the identifier "program" in lowercase letters.

You must make sure that rtl.run() has been called when the page loads.

Show quoted text 9 lines
and the Btn1Click function write it with pas2js. example function Btn1Click(aEvent: TJSMouseEvent): boolean; begin ? //? end; How can I do?

Just write your function, and refer to it in the above manner.

Michael.

Reply by email

Michael Van Canneyt
to Mattias Gaertner: On Tue, 3 Sep 2019 12:24:21 +0200 For example: var Btn3: TJSHTMLButtonElement;…

OP is not subscribed to the list. In your replies, put him in CC.

As I understood the question, it is not how can I set the event in pas2js but how can I refer to a function from HTML.

Michael.

Mattias Gaertner wrote 31 lines
On Tue, 3 Sep 2019 12:24:21 +0200 Gabriele Cappelletto via Pas2js wrote: > Hello > I would like to find a way to call a function written with pas2js from > html code (do not define onclick from pas2js, but call from html) > > example > html > ?<input type="button" class="form-control" id="btn3" value="direct > mode" onclick="Btn1Click();"/> > > and the Btn1Click function write it with pas2js. > example > function Btn1Click(aEvent: TJSMouseEvent): boolean; > begin > ? //? > end; > > How can I do? For example: var Btn3: TJSHTMLButtonElement; Btn3:=TJSHTMLButtonElement(document.getElementById('btn3')); ; Mattias _______________________________________________ Pas2js maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/pas2js

Reply by email

Gabriele Cappelletto
to Michael Van Canneyt: All functions live under the top-level "pas" object, per unit. So…

I get this error when calling pas.test.Btn1Click () ;. "TypeError: Cannot read property 'Btn1Click' of undefined".
I am attaching the .js file and the html.

I have another question. If I write a function not used in the program, the compiler does not compile it in the .js file, is there a way to force the compilation even if the function is not used?

Thank you

Il 04/09/19 08:24, Michael Van Canneyt ha scritto:

Show quoted text 47 lines
On Tue, 3 Sep 2019, Gabriele Cappelletto via Pas2js wrote: > Hello > I would like to find a way to call a function written with pas2js from > html code (do not define onclick from pas2js, but call from html) > > example > html > ?<input type="button" class="form-control" id="btn3" value="direct mode" > onclick="Btn1Click();"/> All functions live under the top-level "pas" object, per unit. So onclick=pas.YourUnit.Yourfunction() should do it. Here YourUnit and YourFunction are of course case sensitive. If the function is in the main program, then you must write onclick=pas.program.Yourfunction() with the identifier "program" in lowercase letters. You must make sure that rtl.run() has been called when the page loads. > > and the Btn1Click function write it with pas2js. > example > function Btn1Click(aEvent: TJSMouseEvent): boolean; > begin > ? //? > end; > > How can I do? Just write your function, and refer to it in the above manner. Michael. _______________________________________________ Pas2js maillist?? -?? https://lists.freepascal.org/cgi-bin/mailman/listinfo/pas2js

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/pas2js/attachments/20190904/8e6991d3/attachment-0001.html> -------------- next part --------------
A non-text attachment was scrubbed...
Name: test.js
Type: application/x-javascript
Size: 42682 bytes
Desc: not available
URL: <http://lists.freepascal.org/pipermail/pas2js/attachments/20190904/8e6991d3/attachment-0001.bin>

Reply by email

Gabriele Cappelletto
to Michael Van Canneyt: All functions live under the top-level "pas" object, per unit. So…

Excuse me, it works with "program".

I have another question. If I write a function not used in the program, the compiler does not compile it in the .js file, is there a way to force the compilation even if the function is not used?

Thank you

Il 04/09/19 08:24, Michael Van Canneyt ha scritto:

Show quoted text 47 lines
On Tue, 3 Sep 2019, Gabriele Cappelletto via Pas2js wrote: > Hello > I would like to find a way to call a function written with pas2js from > html code (do not define onclick from pas2js, but call from html) > > example > html > ?<input type="button" class="form-control" id="btn3" value="direct mode" > onclick="Btn1Click();"/> All functions live under the top-level "pas" object, per unit. So onclick=pas.YourUnit.Yourfunction() should do it. Here YourUnit and YourFunction are of course case sensitive. If the function is in the main program, then you must write onclick=pas.program.Yourfunction() with the identifier "program" in lowercase letters. You must make sure that rtl.run() has been called when the page loads. > > and the Btn1Click function write it with pas2js. > example > function Btn1Click(aEvent: TJSMouseEvent): boolean; > begin > ? //? > end; > > How can I do? Just write your function, and refer to it in the above manner. Michael. _______________________________________________ Pas2js maillist?? -?? https://lists.freepascal.org/cgi-bin/mailman/listinfo/pas2js

Reply by email

Michael Van Canneyt
to Gabriele Cappelletto
Gabriele Cappelletto wrote 6 lines
Excuse me, it works with "program". I have another question. If I write a function not used in the program, the compiler does not compile it in the .js file, is there a way to force the compilation even if the function is not used?

Compile with -O-

Probably this is the reason why you got the HTML error saying your function does not exist: the compiler removed the unit.

Michael.

Reply by email

Mattias Gaertner
to Gabriele Cappelletto: Excuse me, it works with "program". I have another question. If I write a function not…

First of all: Please subscribe to this list:
https://lists.freepascal.org/cgi-bin/mailman/listinfo/pas2js

[...] I have another question. If I write a function not used in the program, the compiler does not compile it in the .js file, is there a way to force the compilation even if the function is not used?

Either disable this optimization (-OoRemoveNotUsedDeclarations) or use it somewhere (you don't need to call it):

if @Btn1Click=nil then ;

Mattias

Reply by email