Modifying the DOM
In the first two tutorials we used no input methods and only printing to the console as the output method.
Now we will explore a more powerful interface: accessing and modifying the DOM.
Accessing the DOM
The client namespace
Browser APIs are declared inside headers which are provided with Cheerp, namely
All classes, global variables and methods exposed by the browser are declared into the client
namespace. It’s a regular C++ namespace, so it’s always possible to write more terse code with:
The document object
You can access the document
global object directly from C++ code. In the next example we will add an event handler to run our code after the DOM is fully loaded. (dom.cpp)
Compiling with:
Now we need a html file:
And opening it (eg. firefox dom.html
) and checking the console log, the output should be something like:
There are two important things to notice:
- the program outlived
webMain()
(while a normal C++ program terminates when themain
returns) (why?) - running code directly from inside
webMain()
can lead to race conditions, where depending on the execution order we may perform invalid operations (since the DOM is not fully formed). The pattern of invoking a callback onDOMContentLoaded
is very important and will be used also in all the following examples, and so it is the more general pattern of usingcheerp::Callback
, that we will now examine.
The cheerp::Callback
adapter function
Callback
is a function defined in the cheerp
namespace which is required to use C++ functions, functors and lambads as callbacks for browser events. For example.
The general usage is:
cheerp::Callbacks are a very powerful instrument:
- they can be attached to any pair of DOM element +
client::Event
to capture mouse movements, clicks, change of focus, button clicks, keyboard buttons pressed, etc. - Callbacks can be attached also to
requestAnimationFrame
orsetTimeout
- Callback could be attached to
DOMContentLoaded
or equivalent events to do initializations (for example, of other callbacks)
Manipulating the DOM
Cheerp works at the same level as JavaScript. It is designed to complement or replace JavaScript as a programming language for the Web. It does not attempt to replace other Web technologies such as HTML and CSS. When using Cheerp you still have to design your page using HTML, CSS and the tools you already know.
If you want to manipulate the DOM at run-time you can use the same APIs you would use when writing JavaScript. In the following example we will create two DOM elements and set up event handling using the DOM APIs exposed by the browser.
Now placing a call to setupInputAndDisplay()
from inside loadCallback()
will add the two elements to the body.
(feel free to modify the previous dom.cpp
. The dom.html
file will remain the same, link)
Even more cheerp::Callback
: buttons & mouse
buttons.cpp is an example of various uses for the cheerp::Callback
to generate buttons, text elements, check mouse position, etc. Compile it to JavaScript, keep using the same dom.html
as before, click on a few buttons.
Recap
We saw how to work with callbacks, a very general instrument that can be used to capture any kind of event.
You can now start experimenting on your own or follow our other tutorials.