HTML
HTML
11.0.1 Objectives
Pencil code offers a very easy drag and drop way to create HTML files. This section introduces the
structure of a simple HTML page. The lessons guide students to create basic pages using block-mode
and to transition to text-mode as their familiarity and knowledge increase.
url Hyper-text
<script> - tag
11.1
Enabling HTML and CSS will split the editing area into multiple panes:
The code for the screenshot above is shown below. It has an HTML page with several elements, two CSS
rules, and two lines of JavaScript code:
cat.moveto(0,0);
JavaScript cat.rt(20);
body { font-family: Arial; }
CSS h1 { text-align: center; }
<html>
HTML <body>
<h1>Introduction to Pencil Code</h1>
<p>Pencil Code is an environment for
learning to code on the web.<p>
<p>It supports both block coding and
text coding with CoffeeScript,
JavaScript, CSS and HTML.</p>
<img id="cat" src="/img/t-cat">
</body>
</html>
All three of the languages in this example interact with each other.
The CSS has a body rule and an h1 rule, setting the visual styles for those HTML elements.
The JavaScript refers to the cat image, using turtle functions to move it and turn it.
This editor can be used to teach web and internet concepts. Because the Pencil Code library is also
provided, all the examples from previous chapters continue to work. Coding can be done directly on a
webpage, allowing students to create animations and interactivity at the same time as learning about
HTML and CSS, URLs, and the Internet.
11.2
The Internet
The Internet is a global network of connected computers acting as clients, routers, and servers. Some of
the most useful data sent on the Internet are webpages, and when some people talk about the Internet,
they are really referring to the Web.
Servers run programs that wait at the network, ready to answer requests that are sent by other
computers. For example, the computer known as “www.un.org” is a server at the United Nations that
waits for requests for webpages, and when it gets a request, it sends back the requested webpage to
whichever computer sent the request.
Clients run programs that make requests by creating and sending messages to servers. For example,
when you use your Web browser on your phone to visit www.un.org, your phone is acting as a client. It
sends a small message with a request to the computer at the UN, and after a moment, it gets back a
message containing a webpage.
Routers run programs that forward messages between other computers. It would be impractical to run a
wire (or a direct radio signal) from your client (and every other client in the world) to the UN. So, instead,
the computer sends messages to a nearby computer with instructions to pass it on in the right direction.
The message is passed on from computer to computer until it arrives at the correct server. Routers are
the silent delivery workers of the Internet. You do not normally need to know the dozen or so routers that
may sit between your client and a server, but if you have noticed a computer on your network known by
the number “192.168.1.1” or “10.0.0.1”, those computers are probably routers.
HTML is the HyperText Markup Language, the main language in which pages on the web are written.
A hypertext document is a text document that embeds links to other resources on the Internet.
A web address is a precise name (written as a URL) that locates one specific document or file on the
Internet.
If one were to draw a picture of a few linked webpages with a dot for each webpage and a line for each
link between them it would look like a Web of connections. The World Wide Web (the Web) is the name
we give to the many billions of linked webpages around the world.
The pages that make up the Web are served by millions of different servers around the world, each
hosting numerous webpages. To understand how a webpage is found, it is important to understand the
URL.
The Web address of a webpage is written in a precise way called a URL (a Uniform Resource Locator).
A URL looks like this:
https://www.un.org/en/index.HTML
The first part “https:” is the protocol, which determines the message formats used when communicating
with the server. There are two main protocols on today’s Web. HTTP (Hyper Text Transfer Protocol)
transmits requests and responses without any encryption so that any router can read the messages.
HTTPS (HTTP Secure) encrypts the messages so that only the client and the server can read them.
11.3
The second part “//www.un.org” after the double slash is the server. Requests for this URL will be sent to
the server www.un.org.
The third part “/en/index.HTML” after the server name is the path of the webpage that will be requested
within the server. Web servers often organize paths using “/” to divide directory names from the files
within them, but Web servers are free to organize their webpages using any paths they choose. For
example, Google serves an almost infinite variety of webpages using URLs such as
“https://www.google.com/search?q=un”. Change the last part of the path from “un” to any other word to
ask Google for a page of search results.
http://newbie.pencilcode.net/home/myprogram
Here the protocol is HTTP unencrypted. (Pencil Code also supports HTTPS.)
The server is “newbie.pencilcode.net”, a server created by the student. All students who save work on
Pencil Code gets their own virtual server. (It is called “virtual” because there is not actually a new
computer for each student: Pencil Code just runs server software using a new name and reuses a
physical computer that is shared with other students.)
The path is “/home/myprogram”, which is “/home/, followed by the name chosen when saving the file. On
Pencil Code, the top-level directory name in the path is special. If you use “/home/”, it will serve the raw
webpage, just like an ordinary Web server. If you change the directory name to “/edit/”, it will serve a
special editing webpage that lets you see the source code and log in to edit the page.
Tags in HTML
HTML (Hyper Text Markup Language) is the language used to write a webpage. A simple HTML
document looks like this:
<!doctype HTML>
<HTML>
<body style="background:wheat">
<h1>My Page</h1>
<p>This is <em>my</em> page.</p>
<p><img src="https://pencilcode.net/img/happyfox" title="little fox"></p>
</body>
</HTML>
A standard HTML document begins with <!doctype HTML> followed by three types of information.
Tags in angles like <body> designate special locations in the document. The tag name is the first word of
the tag.
Attributes of a tag, also written within the angle of the tag, such as title=”little fox” or
style=”background:wheat”.
Text content such as “My Page” which does not appear inside angles.
Most of the tags are paired: a begin tag like <body> matches end tag with a slash like </body>, but there
are a few special self-closing tags such as <img> that are not paired. A tag and its pair (if any) together
11.4
are called an element, and elements may be nested within each other. For example, within the
document above, the <img> element is nested within a <p> element, nested within <body>, which is
nested within the <HTML>. The <em> element is also nested within a different <p> element.
HTML has about 100 types of elements and 100 or so attributes that let programmers do a great variety
of things in a document. The best way to learn about HTML is to make webpages while trying out different
elements described in one of the many online resources available on the Internet. Here is a list of a few
particularly useful elements.
<a href="url">...</a> A hyperlink that leads to a page located by the href url. text
<img src="url"> An image that is loaded from the url in the src attribute. embedding
<iframe src="url"></iframe> A subframe containing a nested page loaded from a url. embedding
The last three elements have attributes href and src whose values are URLs that link to other files or
webpages. On a webpage, there is a short way to write URLs that is important to understand.
Relative URLs
URLs that in webpages can be written in an abbreviated form called relative URLs, which omit portions
of the URL and assume they take on the same values as the current Web address. Ordinary complete
URLs contain a protocol, server name, and path and are called absolute URLs. For example, suppose
the current URL is http://newbie.pencilcode.net/home/myproject/welcome.html. Here are examples of
relative URLs.
11.5
Relative URL Relative to http://newbie.pencilcode.net/home/myproject/welcome.html
friends.html http://newbie.pencilcode.net/home/myproject/friends.html
css/style.css http://newbie.pencilcode.net/home/myproject/css/style.css
/home/index.html http://newbie.pencilcode.net/home/index.html
/img/happyfox http://newbie.pencilcode.net/img/happyfox
//un.org/fr/index.html http://un.org/fr/index.html
https://google.com/ https://google.com/
The basic rule is this: an absolute URL always starts with a protocol name such as http: or https:. If a
relative URL starts with two slashes, it inherits the current protocol but replaces everything after that. If a
relative URL starts with one slash, it replaces everything after the current server name. If a relative URL
does not start with a slash, it replaces everything after the last slash in the current URL.
Absolute URLs can be used anywhere a URL is required, but relative URLs can be much faster to type.
Relative URLs also have the advantage in that if a directory of webpages is moved together between
servers or directories, URLs that make relative references within the directory will continue to work.
Browsers come with default visual styling for every HTML element. For example, the <em>...</em>
element indicates a phrase that should be emphasized, and browsers will use italic font-style for that text
by default. But what if you wish to use a normal font-style and underline the text instead? Visual styles
can be overridden by using the style attribute on any visible element, as follows:
<em style="font-style:normal;text-decoration:underline">something</em>
The value of the style attribute is a style declaration block, which can list any number of style
declarations separated by semicolons. Each style declaration has a style property followed by a colon
and a value. There are about 100 standard style properties and there are many Internet resources that list
them and give examples of how they work.
If you wish to apply the same style declaration block to every <em> element in the document, you can
create a <style> element containing CSS (Cascading Style Sheet) rules, such as:
<style>
em {
font-style:normal;
text-decoration:underline;
}
</style>
11.6
CSS is a powerful language that provides ways to combine and generalize style rules. We will not talk
much about it here but there are excellent resources available on the Internet detailing beautiful effects
that can be created with CSS.
When combining HTML and JavaScript code, the JavaScript is embedded in a <script> element. Pencil
Code provides separate editing panels for separating editing HTML and JavaScript for a webpage, but
when you view them together, Pencil Code puts the JavaScript inside the HTML page by adding a
<script> element.
The <script> Element also can also be used with other languages. When the CoffeeScript language is
loaded, the type attribute may be set so that <script type="text/coffeescript">...</script> contains
CoffeeScript.
1 Day Lesson Plan IV: HTML – JavaScript using arrays (JavaScript embedded in
the webpage)
11.1.3 Standards
11.7
11.1.4 Lesson Plan I
Build a one-page HTML page using basic blocks and HTML tags. Check to see that HTML and CSS
options are checked under settings.
11.8
Content details Teaching Suggestions Time
11.9
11.1.6 Lesson Plan III
Students will now write a JavaScript program that is embedded in the webpage they have been
modifying. The Art Gallery page. Note, the program is a very simple one because it takes what has been
taught in Lesson Plan II and adds the JavaScript information to it.
Code: CSS
body {
font-family: sans-serif;
text-align: center;
}
button {
background: cornflowerblue;
color: white;
border: none;
border-radius: 6px;
font-size: 18px;
margin: 8px;
}
Encourage students to create their own JavaScript code and view the program execution embedded in a
webpage.
Student Practice: 25 minutes
11.10
11.1.7 Lesson Plan IV
This lesson focuses on the creation of a slide show using the JavaScript program. It demonstrates the
use of arrays (the simplest data structure in programming). This culminating lesson enables students to
put together all of the techniques presented in all the lesson plans in this chapter to create an interesting
webpage.
11.11
Content details Teaching Suggestions Time
Code: JavaScript
document.getElementById('bb').addEventList
ener('click', goback);
document.getElementById('rs').addEventList
ener('click', restart);
document.getElementById('ff').addEventList
ener('click',goforward);
var food = [
"/img/orange juice", "/img/cupcake",
"/img/potato chips",
]
var pointer =0;
function goforward() {
pointer += 1;
if (pointer >= food.length) {
pointer =0;
}
document.getElementById('im').src =
(food[pointer]);
}
function goback() {
pointer -= 1;
if (pointer < food.length) {
pointer =food.length-1;
}
document.getElementById('im').src =
(food[pointer]);
function restart(){
document.getElementById('im').src =
food[pointer];
pointer = 0;
Encourage students to change the images. Increase the size of the array and add more elements.
Encourage students to tinker with the CSS and HTML values to improve the visual appeal of the
program.
Student Practice:120 minutes
11.12