Page 1 of 1

use of subs in html

Posted: Tue Feb 20, 2024 12:50 pm
by Stuart
Currently we can use gosub subroutines within html e.g. to perform functions when buttons are pressed. For example, where 'boost_on' is a gosub:

a$ = a$ + "<td>"+ button$(" +1 hours ", boost_on, "mode_text")+"</td>"

If however a sub is used instead the error 'label not found' is reported.

It would be nice to be able to specify the name of a sub, with arguments, in place of the gosub. For example

a$ = a$ + "<td>"+ button$(" +1 hours ", boost "on", "mode_text")+"</td>"

The "on" could be any argument and be helpful where a variable might allow simplified code outside the html when many values might be possible.

Re: use of subs in html

Posted: Sat Feb 24, 2024 8:53 pm
by cicciocb
It is not so straightforward to implement; the risk is to break something that already works.

However, have you considered using the variable HtmlEventButton$?

It contains the name of the button, so you can employ the same gosub routine for several buttons and execute the appropriate action depending on the button pressed.

There are also other ways to accomplish this using JavaScript, but that is likely to be too complex.

Re: use of subs in html

Posted: Sat Feb 24, 2024 9:33 pm
by cicciocb
Just for demonstration, this is how you can do mixing basic, html and javascript.

Code: [Local Link Removed for Guests]

CLS
but1$ = ""
newButton "BUTTON1", |mysub "ping"|, but1$
but2$ = ""
newButton "BUTTON2", |mysub "pong"|, but2$
HTML but1$ + " " + but2$
wait
 
sub newButton name$, cmd$, ret$
ret$ = |<button onclick='connection.send(`cmd:goto| + cmd$ + |`)'>| + name$ + "</button>"
end sub

sub MySub a$
wlog "Inside MySub", a$
end sub

nb :
take care of the quotes, there are all sort of ( ' ` " )

Re: use of subs in html

Posted: Sat Feb 24, 2024 10:20 pm
by Electroguard
Thanks, that is a very useful example.

Re: use of subs in html

Posted: Sun Feb 25, 2024 7:09 am
by cicciocb
Finally this code can be used for a larger scope as you can include any basic command directly in the javascript.

Example :

Code: [Local Link Removed for Guests]

CLS
but1$ = ""
newButton "BUTTON1", |mysub "ping"|, but1$
but2$ = ""
newButton "BUTTON2", |mysub "pong"|, but2$
but3$ = ""
newButton "BUTTON3", |a=100:wlog a*2|, but3$
but4$ = ""
newButton "BUTTON4", |b=50:c=30:wlog "The product is ";b*c|, but4$
HTML but1$ + " " + but2$ + " "  + but3$ + " "  + but4$
wait
 
sub newButton name$, cmd$, ret$
ret$ = |<button onclick='connection.send(`cmd:goto| + cmd$ + |`)'>| + name$ + "</button>"
end sub

sub MySub a$
wlog "Inside MySub", a$
end sub

Re: use of subs in html

Posted: Sun Feb 25, 2024 3:04 pm
by lyizb
Wow. Eye-opening.

Re: use of subs in html

Posted: Wed May 01, 2024 7:52 pm
by Stuart
Useful, I will try this.