back

Lecture 4 (11.07.20) - Organization

Recorded Lecture

Favorite Guides

Try checking out these tutorials if you’d like more guidance!

Introduction to JavaScript

JavaScript is a language, interpreted by your brower, run on your computer. It can manipulatate styles, attributes, and contents of any HTML element.

Assuming that you already have an understanding of either Python, Java, or C/C++, JavaScript is incredibly easy to pick up, syntax-wise. The hardest part will be how to instantiate and organize it. That is what this lecture will cover.

Calling a JavaScript Function

The way to call a JavaScript function is by giving an HTML element an event that needs to be handled. Example events include onclick, onload, and onchange. You can see the complete list here:

Here is an example of a page that gives an alert when the button is pressed:

index.html
script.js

Passing this as a Parameter

To get the properties of the button clicked, just pass this as its parameter.

For example, this page displays a secret message stored in the value attribute in each button:

index.html
script.js

If you would like real example of this in action, look at this calculator app.

JavaScript File Template

Carrying the theme of last week, (organization), JavaScript files need to be organized. Here is how to lay out your file.

  1. List all Global Variables prefaced with var.
  2. Have a function initialize(), where you initialize all your Global Variables and call any functions you need.
  3. Declare and impliment all functions your page uses.

For example:

script.js

Final Thoughts

JavaScript is incredibly powerful and can manipulate anything on the HTML page. Now that you know how to call JavaScript functions, its possibilites are endless! Have fun :)