Harvard's Javascript Course in PDF
Harvard's Javascript Course in PDF
Web
Programming
JavaScript
CS50’s Web Programming with
Python and JavaScript
Introduction
So far, we’ve discussed how to build simple web pages using HTML
and CSS, and how to use Git and GitHub in order to keep track of
changes to our code and collaborate with others. We also
familiarized ourselves with the Python programming language,
started using Django to create web applications, and learned how
to use Django models to store information in our sites.
Today, we’ll introduce a new programming language: JavaScript.
JavaScript
alert('Hello, world!');
The alert function in JavaScript displays a message to the user which
they can then dismiss. To show where this would fit into an actual
HTML document, here’s an example of a simple page with some
JavaScript:
Events
Variables
JavaScript is a programming language just like Python, C, or any
other language you’ve worked with before, meaning it has many
of the same features as other languages including variables. There
are three keywords we can use to assign values in JavaScript:
var: used to define a variable globally
For an example of how we can use a variable, let’s take a look at a page
that keeps track of a counter:
querySelector
One thing to notice about what we’ve just done is that we’re not
calling the count function by adding parentheses afterward, but
instead just naming the function. This specifies that we only wish to
call this function when the button is clicked. This works because, like
Python, JavaScript supports functional programming, so functions
can be treated as values themselves.
The above change alone is not enough though, as we can see by
inspecting the page and looking at our browser’s console:
We can use the function to only run the code once all content has
loaded:
For our counter page, we could have a file called counter.html that
looks like this:
And a file called counter.js that looks like this:
Let’s get started on another example of a page that can be a bit more
interactive. Below, we’ll create a page where a user can type in their
name to get a custom greeting.
Some notes about the page above:
We use the autofocus field in the name input to indicate that the
cursor should be set inside that input as soon as the page is
loaded.
We use #name inside of document.querySelector to find an
element with an id of name. We can use all the same selectors in
this function as we could in CSS.
We use the value attribute of an input field to find what is
currently typed in.
We can do more than just add HTML to our page using JavaScript, we
can also change the styling of a page! In the page below, we use
buttons to change the color of our heading.
Some notes on the page above:
The console is a useful tool for testing out small bits of code and
debugging. You can write and run JavaScript code in the console,
which can be found by inspecting element in your web browser and
then clicking console. (The exact process may change frome browser
to browser.) One useful tool for debugging is printing to the console,
which you can do using the console.log function. For example, in the
colors.html page above, I can add the following line:
We can also have named functions that use arrows, as in this rewriting
of the count function:
To get an idea about some other events we can use, let’s see how we
can implement our color switcher using a dropdown menu instead of
three separate buttons. We can detect changes in a select element
using the onchange attribute. In JavaScript, this is a keyword that
changes based on the context in which it’s used. In the case of an
event handler, this refers to the object that triggered the event.
There are many other events we can detect in JavaScript including the
common ones below:
onclick
onmouseover
onkeydown
onkeyup
onload
onblur
…
TODO List
To put together a few of the things we’ve learned in this lecture, let’s
work on making a TODO list entirely in JavaScript. We’ll start by
writing the HTML layout of the page. Notice below how we leave space
for an unorderd list, but we dont yet add anything to it. Also notice
that we add a link to tasks.js where we’ll write our JavaScript.
Now, here’s our code which we can keep in tasks.js. A few notes on
what you’ll see below:
One thing to notice about all of our sites so far is that every time we
reload the page, all of our information is lost. The heading color goes
back to black, the counter goes back to 0, and all of the tasks are
erased. Sometimes this is what we intend, but other time’s we’ll want
to be able to store information that we can use when a user returns to
the site.
Let’s look at how we can use these new functions to update our
counter! In the code below,
APIs
JavaScript Objects
I can then access or change parts of this object using either bracket or
dot notation:
One way in which JavaScript Objects are really useful is in transferring
data from one site to another, particularly when using APIs
The values within a JSON do not have to just be strings and numbers
as in the example above. We can also store lists, or even other
JavaScript Objects:
Currency Exchange
Rather than simply logging this data, we can use JavaScript to display
a message to the screen, as in the code below:
Now, let’s make the site a bit more interactive by allowing the user to
choose which currency they would like to see. We’ll start by altering
our HTML to allow the user to input a currency:
Now, we’ll make some changes to our JavaScript so it only changes
when the form is submitted, and so it takes into account the user’s
input. We’ll also add some error checking here:
That’s all for this lecture! Next time, we’ll work on using JavaScript to
create even more engaging user interfaces!