Javascript basics

19 February 2019   PDF version

Adding scripts to a page

We include JS code within HTML either by embedding it internally in a script tag:

<script>
console.log("embedded code")
</script>

Or by referencing an external file with the src attribute:

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

And then file.js would contain:

console.log("external code")

Browser console

When developing client-side (in-browser) apps in Javascript, it’s very important to check on the browser console, part of the developer tools.

firefox-console.png

Figure 1: Open the console to see error messages, log messages, to try out JS functions, and browse results

DOM

  • document.getElementById
  • document.getElementsByTagName
  • document.getElementsByClass
  • .nextElementSibling
  • .parent
  • .children

See jsdemo in the public repository in gitlab for examples.

whitney-js.svg

Figure 2: @nonprofWHIT on Twitter