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.
use of subs in html
- cicciocb
- Site Admin
- Posts: 2060
- Joined: Mon Feb 03, 2020 1:15 pm
- Location: Toulouse
- Has thanked: 439 times
- Been thanked: 1358 times
- Contact:
Re: use of subs in html
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.
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.
- cicciocb
- Site Admin
- Posts: 2060
- Joined: Mon Feb 03, 2020 1:15 pm
- Location: Toulouse
- Has thanked: 439 times
- Been thanked: 1358 times
- Contact:
Re: use of subs in html
Just for demonstration, this is how you can do mixing basic, html and javascript.
nb :
take care of the quotes, there are all sort of ( ' ` " )
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
take care of the quotes, there are all sort of ( ' ` " )
- Electroguard
- Posts: 860
- Joined: Mon Feb 08, 2021 6:22 pm
- Has thanked: 276 times
- Been thanked: 323 times
- cicciocb
- Site Admin
- Posts: 2060
- Joined: Mon Feb 03, 2020 1:15 pm
- Location: Toulouse
- Has thanked: 439 times
- Been thanked: 1358 times
- Contact:
Re: use of subs in html
Finally this code can be used for a larger scope as you can include any basic command directly in the javascript.
Example :
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