W3C APIs
Tabris implements a subset of popular W3C standards. Besides providing web developers with familiar APIs, this also enables you to use libraries developed for a browser environment.
window object
In JavaScript there is always an object that represents the global scope. All global variables are members of this object. As in a web browser, this object is named window
in Tabris.js. For example, the tabris object can be accessed either via tabris
or window.tabris
.
console object
As in the browser, messages can be logged to the developer console using the global console
object:
console.log("A log message");
console.error("An error Message");
console.warn("A warning message");
console.info("An info message");
console.debug("A debug Message");
Calling console.error
will also cause a message to pop up (even if the developer console is closed), but it won’t interrupt script execution.
Timer
Tabris supports the timer methods setTimeout
, setInterval
, clearTimeout
and clearInterval
. See window.
XMLHttpRequest
Tabris supports the XMLHttpRequest
to make HTTP request and to read resources that are part of the application.
Tabris.js specifics:
- Only asynchronous requests are supported. Attempting a synchronous request will cause an error.
- When a relative URL is given, Tabris.js will interpret it as a path relative to the application’s
package.json
. This allows you to read static resources (files residing in your project folder). - When using a custom built developer client, a relative URL may be used to access local files (bundled with the client as a resource) as well as those residing in the remote project folder (from which the code is loaded via HTTP). Local files take precedence.
- To enable access to SSL protected resources that use self signed certificates, use the
UseStrictSSL
preference in the config.xml. See the Cordova documentation.
Fetch
As a more powerful and flexible alternative to XHR, you can also use the Fetch API. As of Tabris.js 1.7, an implementation of this API is included.
To load static resources, working with URLs relative to the current module may be more convenient. In the case of a JSON file, this can be done simply by using the
require
method instead of XHR. For other types of files, use the__dirname
variable, e.g.xhr.open("GET", __dirname + "/foo.txt");
.
WebSocket
WebSockets are an advanced technology that makes it possible to open an interactive communication session between the user’s client and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.
As of Tabris.js 1.10, an implementation of this API is included.
The Tabris.js implementation supports to send and receive text messages as well as binary data in the form of
TypedArray
andArrayBuffer
. We currently do not support to receive data asBlob
.
Further documentation:
- https://tools.ietf.org/html/rfc6455
- https://html.spec.whatwg.org/multipage/comms.html#websocket
Persistent Storage
Tabris supports the global object localStorage
, which allows storing key-value pairs in a persistent store. Both keys and values are stored as strings.
On iOS, there is an additional object secureStorage
available in the global scope. This is a drop-in replacement for localStorage
that keeps data in the encrypted iOS Keychain.
Tabris.js specifics:
- Currently,
localStorage
andsecureStorage
support the methodssetItem
,getItem
,removeItem
, andclear
. - The
sessionStorage
is not supported, as it would serve no purpose in a non-browser environment. - The storage event is currently not supported.
The
localStorage
is only meant to store relatively short strings. To store larger amounts of data it is recommended to use the cordovaFileSystem
plugin.
Canvas Context
The Canvas
widget provides an HTML5 canvas compatible “2D Context” object. See Canvas.
Random Source (Crypto)
The global object crypto
provides an implementation of the RandomSource interface. It can be used to generate cryptographically secure random numbers.