1 2 3 4 5 6 7 8 9 | <!DOCTYPE html>
<html>
<head>
<title>Carl Fredricksen's Profile</title>
</head>
<body>
Welcome to Carl Fredricksen's Profile page!
</body>
</html>
|
<X> <Y> ... </Y> </X>
<html>
, which has two child tags: <head>
and <body>
<title>
is a child tag of <head>
<body>
tag1 2 3 4 5 6 | <body>
Welcome to Carl Fredricksen's Profile page!
<img src='img/mugshot.jpg'>
Click <a href='http://pixar.wikia.com/wiki/Carl_Fredricksen'>here</a>
to go to my wiki page.
</body>
|
name='value'
<img>
has no closing tag1 2 3 4 5 6 7 8 9 10 11 12 | <body>
<h1>Welcome to Carl Fredricksen's Profile page!</h1>
<p><img src='img/mugshot.jpg'></p>
<h3>Links</h3>
<p>Click <a href='http://pixar.wikia.com/wiki/Carl_Fredricksen'>here</a>
to go to my wiki page.
</p>
<h3>Hobbies</h3>
<p>I <strong>love</strong> travelling,
<em>especially</em> by balloons.
</p>
</body>
|
<h1>
to <h5>
<p>
organizes text into paragraphs<em>
(emphasis) is for italics, <strong>
is for bold1 2 3 4 5 6 7 8 9 10 11 | <table>
<tr> <td>Name</td> <td>Carl Fredricksen</td> </tr>
<tr> <td>Age</td> <td>78</td> </tr>
<tr> <td>Occupation</td> <td>Retiree</td> </tr>
<tr> <td>Email</td>
<td><a href='mailto:carl@paradisefalls.org'>
carl@paradisefalls.org
</a>
</td>
</tr>
</table>
|
1 2 3 4 5 6 7 8 9 10 | <h3>Friends</h3>
<ul>
<li>Russell</li>
<li>Dug</li>
<li>Kevin</li>
</ul>
<h3>Countries travelled</h3>
<ol>
<li>Venezuela</li>
</ol>
|
<ul>
is unordered list - bullets are not numbered<ol>
is ordered list - bullets are numbered<li>
is list item, can be used in either <ol>
or <ul>
Use the following structure to keep things neat and allow for future expansion
my_profile_dir
|
|-- css
| |
| |-- main.css
|
|-- img
| |
| |-- mugshot.jpg
|
|-- index.html
1 2 3 4 | <head>
<title>Carl Fredricksen's Profile</title>
<link rel='stylesheet' type='text/css' href='css/main.css'>
</head>
|
<div>
HTML element1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <body>
<div id='container'>
<div id='header'>
</div>
<div id='overview'>
</div>
<div id='mugshot'>
</div>
<div id='detail'>
</div>
<div id='footer'>
</div>
</div>
</body>
|
id
attribute<div>
s1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <div id='container'>
<div id='header'>
<h1>Welcome to Carl Fredricksen's Profile page</h1>
</div>
<div id='overview'>
<table>
<tr><td>Name</td> <td>Carl Fredricksen</td></tr>
<tr><td>Age </td> <td>78</td></tr>
<tr><td>Occupation</td> <td>Retiree</td></tr>
<tr><td>Email</td>
<td><a href='mailto:carl@paradisefalls.org'>
carl@paradisefalls.org</a></td>
</tr>
</table>
</div>
...
</div>
|
CSS
1 2 3 4 5 | body
{
color: #333333;
background-color: #a7a09a;
}
|
body
is called the selector{...}
contains properties and values being set#333333
and #a7a09a
are RGB color codes00
to ff
. Quiz: how many colors can RGB represent?body { ... }
#header { ... }
.someclass { ... }
(will see it in chapter 3)div#header { ... }
div h1 { ... }
#header h1, #header h2 { ... }
Can you guess what each of selectors in the above examples do?
div#header { ... }
<div id='header'>
element is selecteddiv h1 { ... }
<h1>
elements that are inside any <div>
element are selected#header h1, #footer h2 { ... }
<h1>
elements that are inside any HTML elements with id='header'
, plus all <h2>
elements that are inside any HTML elements with id='footer'
are selectedLet's give each <div>
a different background color. This will help our eyes when we re-arrange them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #header h1
{
font-family: 'Impact';
}
#overview
{
...
font-family: 'Helvetica';
font-size: 20pt;
}
#detail
{
...
font-family: 'Helvetica';
font-size: 16pt;
}
#footer
{
font-family: 'Courier New';
font-size: 12pt;
}
|
1 2 3 4 5 6 7 8 9 | #container
{
max-width: 750px;
margin-top: 0;
margin-bottom: 0;
margin-left: auto;
margin-right: auto;
background-color: #99c;
}
|
max-width
auto
for margin<div>
s side-by-side1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #overview
{
float: left;
width: 550px;
}
#mugshot
{
float: right;
width: 200px;
}
#mugshot img
{
display: block; /* what does this line do??? ;-) */
max-width: 100%;
max-height: 100%;
}
|
float
allows elements to be side by side, instead of one after anotherdisplay: block
and see what happens. Explanation1 2 3 4 5 | #detail
{
...
clear: both;
}
|
clear: both;
1 2 3 4 5 | div
{
padding-top: 0.1px;
padding-bottom: 0.1px;
}
|
Add paddings to make things look less crammed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #overview
{
padding-top: 5px;
padding-bottom: 5px;
padding-left: 15px;
padding-right: 15px;
}
#detail
{
padding: 5px 15px;
}
#footer
{
padding: 5px 15px;
}
|
Need to subtract left and right padding from width
1 2 3 4 5 6 7 8 | #overview
{
width: 520px; /* was 550px */
padding-top: 5px;
padding-bottom: 5px;
padding-left: 15px;
padding-right: 15px;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | body
{
margin: 0;
}
#header h1
{
text-align: center;
}
td#fieldname
{
width: 40%;
}
#overview
{
background-color: #9cc;
}
|
The scripting language for the web, often used in browsers (called front-end scripting).
We can use Javascript to do many things
Use the following structure to keep things neat and allow for future expansion
my_site_dir
|
|-- css
| |
| |-- main.css
|
|-- js
| |
| |-- main.js
|
|-- img
| |
| |-- mugshot.jpg
|
|-- index.html
Use the <script>
tag (usually add it after the </body>
tag)
1 2 3 4 5 6 7 | <html>
...
<body>
...
</body>
<script src='js/main.js'></script>
</html>
|
/(^.^)\
index.html
1 2 3 4 5 6 7 8 9 10 | <!DOCTYPE html>
<html>
<head>
<title>My Javascript sandbox</title>
</head>
<body>
<div id='myDiv'></div>
</body>
<script src='js/main.js'></script>
</html>
|
js/main.js
1 2 3 4 | var myDiv = document.getElementById('myDiv');
var myP = document.createElement('p');
myP.innerText = '/(^.^)\\';
myDiv.appendChild(myP);
|
/(^.^)\
what's going on?<div>
and give it an unique ID 'myDiv'
<div>
using document.getElementByID()
<p>
element using document.createElement()
<p>
's innerText
property to puppy face<p>
to myDiv
as a child using myDiv.appendChild()
var
to declare a variable=
to assign value to an variable or its property'
(or double quote "
) to enclose a string of characters1 2 3 4 5 6 7 | var myDiv = document.getElementById('myDiv');
for (var i = 0; i < 10; i++)
{
var myP = document.createElement('p');
myP.innerText = i + ': ' + '/(^.^)\\';
myDiv.appendChild(myP);
}
|
for ( [Initial Expression]; [Condition]; [Step Expression]) { ACTION }
i++
is a shorthand for i = i + 1
+
does addition when both operands are numbers; when any of them is a string, it does string concatenation1 2 3 4 5 6 7 8 9 10 11 12 13 14 | var myDiv = document.getElementById('myDiv');
for (var i = 0; i < 10; i++)
{
var myP = document.createElement('p');
if (i === 5)
{
myP.innerText = i + ': ' + '=(^.^)=';
}
else
{
myP.innerText = i + ': ' + '/(^.^)\\';
}
myDiv.appendChild(myP);
}
|
if ( [Condition] ) { Action A } else { Action B }
i++
is a shorthand for i = i + 1
true
or false
Operator | Meaning |
---|---|
a === b | Is a equal b? |
a != b | Is a NOT equal b? |
a > b | Is a greater than b? |
a >= b | Is a greater than or equal to b? |
a < b | Is a less than b? |
a <= b | Is a less than or equal to b? |
true
and false
themselves are booleans+
or -
work on numbers
Name | Operator | Meaning |
---|---|---|
AND | a && b | true if both a and b are true, false otherwise |
OR | a || b | true if either a or b is true, false otherwise |
NOT | ! a | true if a is false , false if a is true |
a=5
and b=7
a > 6 && b > 6
a != 7 || b === 7
(a + b) <= 12
!((a - b) > 0)
a < 8 && b < 8 && (a + b) <= 12
Why? To reuse code, and to reduce code complexity.
A function has 3 parts: a name, a list of parameters and a body.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | function drawPuppiesAndKitty(where, howMany)
{
var myDiv = document.getElementById(where);
for (var i = 0; i < howMany; i++)
{
var myP = document.createElement('p');
if (i === 5)
{
myP.innerText = i + ': ' + '=(^.^)=';
}
else
{
myP.innerText = i + ': ' + '/(^.^)\\';
}
myDiv.appendChild(myP);
}
}
|
HTML
1 2 3 4 5 6 | ...
<body>
<div id='myDiv'><p>Here are some furry friends</p></div>
<div id='myDiv2'><p>Another bunch of furry friends</p></div>
</body>
...
|
Javascript
1 2 3 4 5 6 7 | function drawPuppiesAndKitty(where, howMany)
{
...
}
drawPuppiesAndKitty('myDiv', 7);
drawPuppiesAndKitty('myDiv2', 6);
|
Think of an array as a row of chairs with numbers (starting from 0) on them
Let's create an array of pets:
1 | var pets = ['P', 'K', 'P', 'P', 'K', 'K']; // P for Puppy, K for Kitty
|
You can access each pet by specifying its index, like var secondPet = pets[1];
Can you write a function to draw an array of pets? Hint: use a for-loop
Create a new <div>
for this exercise
1 | <div id='myDiv3'><p>An array of pets</p></div>
|
We will declare and call our new function this way
1 2 3 4 5 6 | function drawPets(where, arrayOfPets)
{
...
}
drawPets('myDiv3', pets);
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | function drawPets(where, arrayOfPets)
{
var myDiv = document.getElementById(where);
for (var i = 0; i < arrayOfPets.length; i ++)
{
var pet = arrayOfPets[i];
var myP = document.createElement('p');
switch (pet)
{
case 'P':
myP.innerText = i + ': ' + '/(^.^)\\';
break;
case 'K':
myP.innerText = i + ': ' + '=(^.^)=';
break;
default:
myP.innerText = i + ': ' + '???';
break;
}
myDiv.appendChild(myP);
}
}
|
switch
is handy when there are more than 2 branches
1 2 3 4 5 6 7 8 9 10 11 12 13 | switch (VARIABLE)
{
case VALUE_1:
ACTION_1;
break;
case VALUE_2:
ACTION_2;
break;
...
default:
ACTION_DEFAULT;
break;
}
|
Try drawing this array of pets: ['P', 'K', 'P', 'P', 'K', 'F']
Allow user to play hide-and-seek with a chosen pet. You need the following:
HTML
1 2 3 4 5 6 7 8 | ...
<div>
<label>Which pet?
<input id='petNumber' type='text' placeholder='Enter pet number'>
</label>
<button onclick='hideAndSeek("myDiv3")'>Hide-and-Seek</button>
</div>
...
|
1 2 3 4 5 6 7 8 9 10 | ...
for (var i = 0; i < arrayOfPets.length; i ++)
{
var pet = arrayOfPets[i];
var myP = document.createElement('p');
myP.id = where + '_' + i;
switch (pet)
{
case 'P':
...
|
Now, take a look in Chrome Developer Tools to inspect the generated content
We will create a class in CSS called hidden
. Any HTML element belong to this class will become invisible.
In CSS, the selector for a class is denoted by a .
in front of its name.
CSS
1 2 3 4 | .hidden
{
visibility: hidden;
}
|
HTML
1 2 3 4 | <head>
<title>My Javascript sandbox</title>
<link rel="stylesheet" type="text/css" href="css/checkpoint_03.css">
</head>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function hideAndSeek(where)
{
var petNumberInput = document.getElementById('petNumber');
var petID = where + "_" + petNumberInput.value;
var petP = document.getElementById(petID);
if (petP.className != 'hidden')
{
petP.className = 'hidden';
}
else
{
petP.className = '';
}
}
|
Check if user has entered a number
1 2 3 4 5 6 7 8 9 10 | function hideAndSeek(where)
{
var petNumberInput = document.getElementById('petNumber');
if (isNaN(petNumberInput.value))
{
alert('Please enter a number!')
return;
}
...
}
|
Try pressing the button without entering anything. Can you fix it?
1 2 3 4 5 6 7 8 9 10 | function hideAndSeek(where)
{
var petNumberInput = document.getElementById('petNumber');
if (isNaN(petNumberInput.value) || petNumberInput.value === '')
{
alert('Please enter a number!')
return;
}
...
}
|
Try entering a pet number that doesn't exist. How do you fix it?
1 2 3 4 5 6 7 8 9 10 11 12 13 | function hideAndSeek(where)
{
...
var petID = where + "_" + petNumberInput.value;
var petP = document.getElementById(petID);
if (petP === null)
{
alert("Pet number doesn't exist!");
return;
}
...
}
|
h1
, p
, em
, strong
, img
, a
, ul
, ol
, table
, div
, input
, button
float
and clear
to create multi-column layoutvisibility
to hide/unhide contentTable of contents | t |
---|---|
Exposé | ESC |
Autoscale | e |
Full screen slides | f |
Presenter view | p |
Source files | s |
Slide numbers | n |
Blank screen | b |
Notes | 2 |
Help | h |