Skip to article frontmatterSkip to article content
tools = require('../js/tools'); tools.init()
Loading...

make sure you read this notebook thoroughly before you start

in this notebook :

assignment

create or reuse a HTML document as a collection of 3 files; it can be your resumé, in that case however you may want to commit them first...

  1. make sure the html <head> loads both the css and js companions

  2. then edit the JavaScript code, so that the background alternates every 1 second between 2 different colours
    note that there are many approaches to achieving this, and at least 2:

    • either changing properties directly on the DOM,

    • or messing with CSS classes
      and early finisher should give both angles a go

in any case you should obtain something like this

Loading...

tip #1 : run code upon load

so, you want to start some code - say call function start() - right after the page loads

the wrong way

it is tempting, but totally unsafe, to do something like

<!-- DO NOT DO THIS -->

<script src="thecode.js">
</script>

<script>
start('some-data')
</script>

because:

the proper way

the proper way is to attach a callback to the page load event

// attach an (anonymous) function to the 'load' event
document.addEventListener(
    'DOMContentLoaded',                   // the event name
    () => start('some-data')  // the callback: must be a function
)

this time, start() will get called later at a time where you can be sure the document is entirely loaded

tip #2: implementing a cyclic task

implementing a cyclic task was done in example 2 already; as a reminder it is based on setInterval():

// not mandatory, but with this soft switch
// we could easily turn the blinking on and off
/*let*/ active = true;

function one_step() {
    if (active)
        console.log("beep");
}

// start the cyclic job: call one_step() every 1s
/*const*/ interval = setInterval(one_step, 1000)

turning it off

quick users may want to implement some sort of trick to turn off the blinking
with the code above, we have 2 options to do that

  1. just do active = false in that case the cyclic task is still there, but does nothing

  2. or cancel the cyclic task altogether, like so

    clearInterval(interval)

tip #3: the browser cache (yet again)

the browser cache thingy applies exactly the same with CSS and with JS

tip #4 : use devel tools

Devel Tools : Elements

as mentioned earlier already, you can

visualizing a changed property

Devel Tools : the Console REPL

Devel Tools : Sources

occasionnally useful to browse the code actually loaded

Devel Tools : debugger

the Sources tab has buit-in debugging features

more on devel tools