Just as you were getting comfortable with Arduino, let’s switch over to making a website!
There are thousands of good resources online for learning basic HTML, CSS, and Javascript, and there are thousands of tools, utilities, libraries, and frameworks to choose from.
For a good introduction to HTML syntax and some basic tags, check out the w3schools HTML tutorial
For this tutorial, we’ll be making an exteremly simple HTML page.
index.html
and open that file in a text editorNow if you double-click on index.html it should open up in a web browser and you should see something like:
For a good introduction to Javascript syntax and some basic functions, check out the w3schools Javascript tutorial
For this tutorial, we’ll be writing an extremely simple Javascript script.
index.html
, create a new file named app.js
and open that file in a text editorJavascript can be added to a HTML page by including <script>
tags which reference external javascript files.
Typically <script>
tags are enclosed within the <body>
tag and are towards the end after all of the content.
Let’s add the jquery javascript library and our own custom javascript file to our page. To do that, replace the contents of the <body>
tag in index.html
with the following:
Now if you reopen or reload index.html, you should see an alert like:
To make web pages which respond to user actions, you first need to add elements for the users to interact with and then you need to connect in a javascript function which can be triggered when that element is interacted with.
As an example first let’s add a <button>
tag to the body. To do that, replace the contents of the <body>
tag in index.html
with the following:
Note that this button will try to run the javascript function myButtonWasClicked()
when it is clicked. Since that function doesn’t yet exist, let’s create it by replacing the contents of app.js
with:
Now if you reopen or reload index.html and click the button labeled Press me!
, you should see an alert like:
Another thing which javascript can do is modify the content of your webpage by either adding, removing, or replacing HTML elements.
As an example first let’s add an empty <div>
tag to the body which we will later update with text using javascript. To do that, replace the contents of the <body>
tag in index.html
with the following:
Now let’s change our button to update the <div>
text instead of showing an alert. In app.js
, replace the myButtonWasClicked()
function with the following:
Now if you reopen or reload index.html and click the button labeled Press me!
, you should modify the page to look like:
Before we proceed, make sure your index.html looks like this and your app.js looks like this.
Close any open copies of your web page that you may have, and then double-click on index.html
to open it up in a new browser window or tab. If you click the button labeled Press me!
, you should modify the page to look like: