(04-22-2025, 10:23 AM)mdijkens Wrote: I'm taking a first look at QB.js to investigate mobile app development.
I can't find documentation or examples for calling/using javascript functions
I've only found a small example using inline javascript with an alert.
I'd like to try for example accessing the GPS, (bluetooth) serial comms, REST calls, etc.
Do these samples/doc exists?
Hi @mdijkens, I'm sure there could be more documentation along these lines. So, I'll write some right here...
I've tried to keep it as simple as possible for calling out to native Javascript from QBJS. The main mechanism for this is the $If Javascript metacommand. Everything between the starting "$If Javascript Then" and "$End If" lines will essentially be included as-is by the compiler and executed as plain Javascript with no transformation.
From within this block you can reference any non-array variables directly that were declared in the Basic code:
Here's another example utilizing the browser's
geolocation API. I usually like to try to wrap calls to native Javascript inside a function or sub that is easy to call from within the rest of the application:
View in QBJS
Code: (Select All)
Dim result As Object
Dim errorMsg As String
If GetPosition(result) Then
Print "Your current position is:"
Print "Latitude : "; result.position.latitude
Print "Longitude: "; result.position.longitude
Print "More or less"; result.position.accuracy; " meters."
Else
Print "ERROR: "; result.errorMsg
End If
Function GetPosition(result As Object)
Dim complete As Integer
$If Javascript Then
const options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0,
};
function onSuccess(pos) {
complete = -1;
result.success = -1;
result.position = pos.coords;
}
function onError(err) {
complete = -1;
result.success = 0;
result.errorMsg = err.message;
}
navigator.geolocation.getCurrentPosition(onSuccess, onError, options);
$End If
' This method processes asynchonously, so we'll wait for the result
While complete = 0
Limit 10
WEnd
GetPosition = result.success
End Sub
This is a great way to make reusable code that can be shared with a wider BASIC programming audience. This is basically the approach that is used for the standard
QBJS Import libraries. You can peruse the source code for these libraries and see more examples here:
https://github.com/boxgaming/qbjs/tree/main/lib.
Let me know if you have additional questions. If you come up with something cool make sure you share it here, maybe we can add it to the standard QBJS libraries.