use of subs in html

Give it a try, it costs you nothing !
Post Reply
Stuart
Posts: 136
Joined: Fri Feb 19, 2021 7:46 pm
Has thanked: 5 times
Been thanked: 20 times

use of subs in html

Post 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.
User avatar
cicciocb
Site Admin
Posts: 1989
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 426 times
Been thanked: 1329 times
Contact:

Re: use of subs in html

Post 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.
User avatar
cicciocb
Site Admin
Posts: 1989
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 426 times
Been thanked: 1329 times
Contact:

Re: use of subs in html

Post 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 ( ' ` " )
User avatar
Electroguard
Posts: 855
Joined: Mon Feb 08, 2021 6:22 pm
Has thanked: 273 times
Been thanked: 321 times

Re: use of subs in html

Post by Electroguard »

Thanks, that is a very useful example.
User avatar
cicciocb
Site Admin
Posts: 1989
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 426 times
Been thanked: 1329 times
Contact:

Re: use of subs in html

Post 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
lyizb
Posts: 97
Joined: Fri Feb 12, 2021 8:23 pm
Has thanked: 40 times
Been thanked: 24 times

Re: use of subs in html

Post by lyizb »

Wow. Eye-opening.
Post Reply