L01-HTML Merged
L01-HTML Merged
HTML
Joseph Tonien
HTML
<html>
<head>
<title>JT</title>
</head>
<body>
Hello World!
</body>
</html>
The first HTML document
<html>
<head>
<title>JT</title>
</head>
<body>
Hello World!
</body>
</html>
HTML document structure
<html>
● A HTML document starts with
<head> <html> and ends with </html>
</body>
</html>
HTML tags
<html>
<head>
tag
<title>JT</title>
</head>
<body>
Hello World!
</body>
</html>
HTML tags
tags are NOT case sensitive
<html>
<head>
tag
<title>JT</title>
</head>
most tag goes in pair
<body>
<tagname>content</tagname>
Hello World!
</body>
start tag end tag
</html>
<h1>Heading 1</h1>
Heading tags: <h1>, <h2>,..., <h6>
<h2>Heading 2</h2>
<h1> the most important heading
<h3>Heading 3</h3>
<h6> the least important heading
<h6>Heading 6</h6>
Normal text...
</body>
Paragraph tag <p>
<body>
<p>This is a paragraph</p>
Extra spaces and lines will NOT
<p>Another be displayed in paragraph
paragraph</p>
<p>yet another
paragraph</p>
</body>
Line break <br />
<body>
<p>This is a paragraph</p>
<br /> tag defines a line break
<p>Another <br />
<br /> is an empty element
paragraph</p> (i.e. it is a tag with no content),
it combines the start and end
<p>yet another <br /> tags together
<br />
paragraph</p>
</body>
Horizontal line <hr />
<body>
<p>This is a paragraph</p>
similarly, we have the
<p>Another <br /> horizontal line tag <hr />
with no content
paragraph</p>
<hr />
<br />
paragraph</p>
</body>
Non-breaking space
<body>
<p>This is a
use for non-breaking space
paragraph</p>
paragraph</p>
<hr />
<br />
paragraph</p>
</body>
Character entity
● Some characters are reserved in HTML.
o ° Degree
© © Copyright
Character entity
<body>
<body>
<p>normal paragraph</p>
<blockquote>
</blockquote>
</body>
Formatting text
<body>
</body>
Formatting text
<body>
</body>
Formatting text
<body>
Some math
x<sub>1</sub><sup>n</sup> +
x<sub>2</sub><sup>n</sup> =
y<sub>1</sub><sup>n</sup> +
y<sub>2</sub><sup>n</sup>
</body>
Preformatted text <pre>
<body>
<pre>
pre element is shown in monospace
Mary
it preserves the character and line spacing
had a
little
lamb
</pre>
</body>
Computer code
<body>
<pre>
<code>
a = 0;
b = 3;
c = 2;
sum = a + b + c;
</code>
</pre>
what would happen if we use <code> … </code>
</body> without <pre> ?
If you want to include special characters such as
Computer code < > & " '
within pre tags, they should be substituted by
<body>
character entities so that they are not subject to
<pre>
special interpretation by the browser.
<code>
#include <iostream>
void main( ) {
cout << "Hello World!" << endl;
}
</code>
</pre>
</body>
#include <iostream>
void main( ) {
cout << "Hello World!" << endl;
}
Image
<body>
</body>
Attribute Description
src URL of an image, for example
src="uow-logo.png"
src="images/uow-logo.png"
src="http://www.mycom.au/staff.png"
height optional.
width Specifies height, width for image in pixels, or in percentage
Image alt
<body>
</body>
Absolute URL
src="http://www.mycom.au/staff.png"
Relative URL
src="images/logo/uow-logo.png"
src="/../f1/bird.png"
Tag attributes
<body>
</body>
</body>
Link
<a href="http://www.uow.edu.au" target="_blank">Visit UOW</a>
target description
href="contact.html"
href="assignment/a1.html"
href="../handout/note5.html"
Link
<a href="http://www.uow.edu.au" target="_blank">
Within the link tag <a href...> </a>, we can put any text or image.
<h3 id="Proofs">Proofs</h3>
…
<h3 id="Notes">Notes</h3>
…
<h3 id="References">References</h3>
…
<h3 id="Proofs">Proofs</h3>
…
The id value must be unique and must contain at least one character.
The id value must not contain any space characters.
Link - target within document
We can create a link to a specific location within a html page
For example:
<a href="https://en.wikipedia.org/wiki/Euler%27s_theorem#Proofs">
Proof of the Euler theorem</a>
Unordered List
My timetable:
<ul>
</ul>
My timetable:
<ol>
</ol>
My timetable:
<dl>
<dt>MATH222</dt>
<dt>CSCI204</dt>
<dt>ISIT206</dt> MATH222
Mon 8:30-10:30 lecture
<dd>Wed 8:30-10:30 lecture</dd>
CSCI204
</dl> Tue 9:30-11:30 lab
ISIT206
Wed 8:30-10:30 lecture
Table
<table border="1">
<tr>
<th>Username</th>
<th>First name</th>
<th>Last name</th>
</tr>
<tr>
<td>jsmith</td>
<td>John</td>
<td>Smith</td>
</tr>
<tr>
<td>mlee</td>
<td>Mary</td>
<td>Lee</td>
</tr>
</table>
border="0"
Table
<table border="1" width="50%">
<caption>User information</caption>
<tr>
<th width="20%">Username</th>
<th width="40%">First name</th>
<th width="40%">Last name</th>
</tr>
<tr>
<td align="center">jsmith</td>
<td align="right">John</td>
<td align="right">Smith</td>
</tr>
<tr>
<td align="center">mlee</td>
<td align="right">Mary</td>
<td align="right">Lee</td>
</tr>
</table>
Table
<table border="1" width="40%">
<tr>
<td colspan="2">STUDENT DETAILS</td>
</tr>
<tr>
<td width="30%">STUDENT NAME</td>
<td>John Lee</td>
</tr>
<tr>
<td>STUDENT NUMBER</td>
<td>1234567</td>
</tr>
<tr>
<td>UOW EMAIL</td>
<td>jlee@uowmail.edu.au</td>
</tr>
</table>
index.html
When we go to http://www.uow.edu.au/~dong/w3/example/html2
● It stops people from knowing the content and structure of your website
I don’t have index.html for the directory html, that is why everybody can see the
content of my directory
http://www.uow.edu.au/~dong/w3/example/html
https://www.lifewire.com/index-html-page-3466505
<body>
<!-- this is
a long comment
-->
</body>
References
http://www.w3schools.com/html
http://developer.mozilla.org/en-US/docs/Web/HTML
CSIT128 / CSIT828
CSS
Joseph Tonien
Cascading Style Sheets
● Inline
● Document
● External
<body style="background-color:lightgrey;">
</p>
this is called a CSS property
Inline CSS
</p>
Inline CSS
</p>
Inline CSS
</p>
property:value
● We can specify more than one CSS property, separated by a semicolon (;)
<html>
<head>
<title>W3</title>
<style>
body {background-color:lightgrey;}
h1 {color:blue;}
p {border:1px solid black; padding:10px;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph</p>
</body>
</html>
External CSS
<html>
<head>
<title>W3</title>
<link rel="stylesheet" href="path/to/mystyle.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph</p>
</body>
</html>
mystyle.css
body {background-color:lightgrey;}
h1 {color:blue;}
p {border:1px solid black; padding:10px;}
Levels of CSS
body {
background-color:lightgrey;
}
h1 {
color:blue;
}
p {
border:1px solid black; each property on
padding:10px; a separate line
}
Simple selector
Example:
What are the descendants
of this element div ? <div>
Some text <i>italic</i> here.
<p>
Hi there <i>italic again</i>
</p>
<div>
This is the final <i>italic</i>.
</div>
</div>
Child-Parent
<E>
…
<F>
…
</E>
Example:
What are the children
of this element div ? <div>
Some text <i>italic</i> here.
<p>
Hi there <i>italic again</i>
</p>
<div>
This is the final <i>italic</i>.
</div>
</div>
Contextual Selector
Example: div i {
color:red;
<div> }
Some text <i>italic</i> here.
<p>
Hi there <i>italic again</i>
</p>
<div>
This is the final <i>italic</i>.
</div>
</div>
Contextual Selector
Example:
<div>
Some text <i>italic</i> here. div > i {
<p> color:red;
Hi there <i>italic again</i> }
</p>
<div>
This is the final <i>italic</i>.
</div>
</div>
Contextual Selector
Example: div.userInfo i {
color:red;
<div class="userInfo"> }
Some text <i>italic</i> here.
<p>
Hi there <i>italic again</i>
</p>
<div class="bankInfo">
This is the final <i>italic</i>.
</div>
</div>
Contextual Selector
Example: div.bankInfo i {
color:red;
<div class="userInfo"> }
Some text <i>italic</i> here.
<p>
Hi there <i>italic again</i>
</p>
<div class="bankInfo">
This is the final <i>italic</i>.
</div>
</div>
Contextual Selector
<ol>
<li>Item 1</li>
<li>Item 2</li>
</ol>
span
span.specialText {
color:red;
font-family:Ariel;
font-size:150%;
}
div
<div class="userInfo">
<p>This section displays user information.</p>
</div>
<div class="bankInfo">
<p>This section display bank information.</p>
</div>
div.userInfo {
border:1px solid black;
padding:10px;
}
div.bankInfo {
background-color:lightgrey;
}
Comments in CSS
A comment starts with /* and ends with */
p {
border:1px solid black;
color:blue;
}
/* This is
a multi-line
comment */
References
http://www.w3schools.com/css
https://en.wikipedia.org/wiki/Cascading_Style_Sheets
Web Form
Joseph Tonien
Form
● HTML forms are used to collect user input.
● When user clicks a submit button, user input will be sent to the server to
process
Form - type text
<form ...>
First name:<br />
<input type="text" name="firstname" size="30"><br />
Last name:<br />
<input type="text" name="lastname" size="30"><br />
...
</form>
Form - type password
<form ...>
Username:<br />
<input type="text" name="username" size="30"><br />
Password:<br />
<input type="password" name="password" size="30"><br />
...
</form>
Form - type checkbox
This is the value that sent to server
<form ...>
Choose journals to subscribe:<br />
<input type="checkbox" name="journal" value="AMM">American
Mathematical Monthly<br />
<input type="checkbox" name="journal" value="CMJ">College
Mathematics Journal<br />
<input type="checkbox" name="journal" value="MM">Mathematics
Magazine<br />
...
</form>
This is the value that get displayed
Form - type radio
This is the value that sent to server
<form ...>
Select student type:<br />
<input type="radio" name="studentType" value="u">Undergraduate
<input type="radio" name="studentType" value="p">Postgraduate
<input type="radio" name="studentType" value="other">Other
...
</form>
<form ...>
Select day:<br />
<select name="day">
<option value="mon">Monday</option>
<option value="tue">Tuesday</option>
<option value="wed">Wednesday</option>
<option value="thu">Thursday</option>
<option value="fri">Friday</option>
</select>
...
</form>
This is the value that get displayed
Form - select multiple
<form ...>
Select day:<br />
<select name="day" multiple>
<option value="mon">Monday</option>
<option value="tue">Tuesday</option>
<option value="wed">Wednesday</option>
<option value="thu">Thursday</option>
<option value="fri">Friday</option>
</select>
...
</form>
Form - textarea
<form ...>
Enter your comment:<br />
<textarea name="comment" rows="5" cols="30">
</textarea>
...
</form>
Form - submit this is the program in the server that processes the form
</form>
Form method
<form action="handle_login" method="post">
method="get" method="post"
Visibility Data is visible in the URL Data is not displayed in the URL
Security get is less secure compared to post because data sent in part of
the URL.
Never use get when sending passwords or other sensitive
information.
method="get" method="post"
</form>
<form ...>
First name:<br />
<input type="text" name="firstname" size="30"><br />
Last name:<br />
<input type="text" name="lastname" size="30"><br />
</form> file:///D:/demo/handle_login?
</body> journal=AMM&
</html> journal=CMJ&
studentType=other&
day=mon&
day=wed
Examples
Google search
http://www.uow.edu.au/~dong/w3/example/form/google-search.html
Google translation
http://www.uow.edu.au/~dong/w3/example/form/google-
translation.html
How can we find out the parameters for Google search and Google translation?
References
http://www.w3schools.com/html
<script>
function sayHi(){
alert("Hi");
}
</script>
Where to include JavaScript
Common practice:
● In the head
● At the end of body
<script>
function sayHi(){
alert("Hi");
}
</script>
Where to include JavaScript
In the head
<head>
<title>JavaScript Example</title>
<script>
function sayHi(){
alert("Hi");
}
</script>
</head>
Where to include JavaScript
...
<script>
function sayHi(){
alert("Hi");
}
</script>
</body>
</html>
External JavaScript
<script>
function sayHi(){
alert("Hi");
}
</script>
<script>
function changeToFrench(){
document.getElementById("french").innerHTML = "Salut!";
}
</script>
Change style by JavaScript
<script>
function changeHelloWorldStyle(){
var e = document.getElementById("hello");
e.style.color = "orange";
e.style.fontSize = "30px";
e.style.fontStyle = "italic";
}
</script>
Basic JavaScript syntax
function changeHelloWorldStyle(){
var e = document.getElementById("hello");
e.style.color = "orange";
e.style.fontSize = "30px";
e.style.fontStyle = "italic";
}
Basic JavaScript syntax
JavaScript Comments
Code after double slashes // or between /* and */ is treated as a comment.
Comments are ignored, and will not be executed.
/*
this function change the style of the text
*/
function changeHelloWorldStyle(){
//get the html element
var e = document.getElementById("hello");
//change the style
e.style.color = "orange";
e.style.fontSize = "30px";
e.style.fontStyle = "italic";
}
Basic JavaScript syntax
JavaScript uses the var keyword to declare variables.
underscore:
student_name, student_id, first_name, last_name
camel case:
studentName, studentId, firstName, lastName
Basic JavaScript syntax
var x; // x is undefined
alert(x);
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
++ Increment
-- Decrement
Basic JavaScript syntax
Assignment operators
var x;
var x;
var x = 5;
var positive = (x > 0);
if(positive){
alert("x is positive");
}
Basic JavaScript syntax
Comparison and Logical Operators
== equal to
=== equal value and equal type
!= not equal
!== not equal value or not equal type
> greater than
< less than
>= greater than or equal to
<= less than or equal to
? ternary operator
Basic JavaScript syntax
== equal to
=== equal value and equal type
var x = 5;
var y = "5";
if(x == y){
alert("yes");
}else{
alert("no");
}
Basic JavaScript syntax
== equal to
=== equal value and equal type
var x = 5;
var y = "5";
!= not equal
!== not equal value or not equal type
var x = 5;
var y = "5";
if(x != y){
alert("yes");
}else{
alert("no");
}
Basic JavaScript syntax
!= not equal
!== not equal value or not equal type
var x = 5;
var y = "5";
var x = 5;
var y = "x is " + ( x%2==0 ? "even" : "odd" );
alert(y);
Date
alert(now);
alert(typeof now); //object
Date
//using YYYY-MM-DDTHH:MI:SS
var d = new Date("2000-01-30T10:00:00");
alert(d);
Date
var text = "One Fish, Two Fish, Red Fish, Blue Fish";
var text = "One Fish, Two Fish, Red Fish, Blue Fish";
var text = "One Fish, Two Fish, Red Fish, Blue Fish";
alert(subjects); //ISIT206,MATH121,CSCI301
!!!
var arrayName = [item0, item1, ...];
alert(subjects[0]); //ISIT206
alert(subjects[1]); //LOGIC101
alert(subjects[2]); //CSCI301
alert(subjects[3]); //LAW201
Array
Length of array
subjects[1] = "LOGIC101";
subjects[3] = "LAW201";
var info = {
name: "John",
dob: new Date("1996-01-20"),
year: 2
}; //it is better to write this way
alert(info.name); //John
alert(info["name"]); //John
obj.property
obj["property"]
Object
Change the values of an object
var info = {
name: "John",
dob: new Date("1996-01-20"),
year: 2
};
// two ways:
info.year = 1;
info["year"] = 1;
Object
Delete object properties
var info = {
name: "John",
dob: new Date("1996-01-20"),
year: 2
};
// two ways:
delete info.year;
delete info["year"];
Object
Create an empty object
var info = { };
info.firstName = "John";
info.lastName = "Lee";
alert(info["firstName"]);
alert(info.lastName);
Array vs Object
arrayName[0] = "LOGIC101";
arrayName[1] = "CSCI111";
objectName["firstName"] = "John";
objectName.lastName = "Lee";
Array Sorting
var subjects = ["ISIT206", "MATH121", "CSCI301"];
subjects.sort();
In general:
the_array_to_be_sorted.sort(the_sorting_function ...);
http://www.uow.edu.au/~dong/w3/example/js/ninja.html
Array Sorting Before sorting
ninja_results = [
ninja_results.sort(
{name: "John", level: 4, seconds: 85},
function (p1, p2) {
{name: "Peter", level: 2, seconds: 35},
if (p1["level"] > p2["level"]){
{name: "Kate", level: 4, seconds: 80},
return 1; // sort
{name: "Luke", level: 5, seconds: 120}
}
];
if (p1["level"] < p2["level"]){
return -1; // don’t sort
}
//at this point the two persons have the same level
if (p1["seconds"] < p2["seconds"]){
return 1; // sort
}
if (p1["seconds"] > p2["seconds"]){
return -1; // don’t sort After sorting
} ninja_results = [
When a confirm box pops up, the user will have to click either "OK" or "Cancel".
If the user clicks "OK" the box returns the input value.
prompt("sometext","defaultText");
http://www.w3schools.com/js
More on JavaScript
Joseph Tonien
Form validation
<form action="myService" method="get"
onSubmit="return validateForm()">
Use form attribute
... your form goes here ... onSubmit to check input
before form submission
</form>
<script>
function validateForm() {
if (... something wrong ...) {
return false;
}
return true;
} When function returns
</script> false, form will not be
submitted
Form validation
Example 1: we want user to fill out the email, if email is not filled out then we will
alert the user
</form>
www.uow.edu.au/~dong/w3/example/js/validation1.html
Form validation
Example 1: we want user to fill out the email, if email is not filled out then we will
alert the user
<script>
function validateForm() {
var email = document.getElementById("email").value;
return true;
}
</script>
Example 2:
http://www.uow.edu.au/~dong/w3/example/js/validation2.html
return true;
}
</script>
If user didn’t fill out the email, we want to display an error message.
We use a span element as a placeholder for the error message.
</form>
Form validation
If user didn’t fill out the email, we want to display an error message.
We use a span element as a placeholder for the error message.
function validateForm() {
var email = document.getElementById("email").value;
return false;
}
return true;
}
<span id="emailError"></span>
Form validation
We want the error message has the color red.
<style>
#emailError{
color: red;
}
</style>
<span id="emailError"></span>
Form validation
Example 4: We want to have two input fields. One for email and another one for
email confirmation. User has to fill in the same email for both input fields.
</form>
http://www.uow.edu.au/~dong/w3/example/js/validation4.html
Form validation
function validateForm() {
var email = document.getElementById("email").value;
if (email == null || email.trim() == ""){
document.getElementById("emailError").innerHTML
= "Email must be filled out";
return false;
}
if(email.trim() != email2.trim()){
document.getElementById("emailError2").innerHTML =
"Email does not matched";
return false;
}
return true;
}
Form validation
We want all the error messages have the color red.
<style>
#emailError{
color: red;
}
#emailError2{
color: red;
}
</style>
<span id="emailError"></span>
<span id="emailError2"></span>
Form validation
Better solution: using class
<style>
.errorMessage{
color: red;
}
</style>
Suppose that user fixed the error by filling out the first email, but leaving the second
email field blank.
When the user clicks Submit, we will see that the error message next to the first
input field still shows.
http://www.uow.edu.au/~dong/w3/example/js/validation4.html
http://www.uow.edu.au/~dong/w3/example/js/validation4b.html
function validateForm() {
var email = document.getElementById("email").value;
function validateForm() {
...
var email2 = document.getElementById("email2").value;
function validateForm() {
...
if(email.trim() != email2.trim()){
document.getElementById("emailError2").innerHTML =
"Email does not matched";
return false;
}else{
document.getElementById("emailError2").innerHTML = "";
}
...
Form validation
Final touch: we want to remove all whitespaces in the two input fields before submit
function validateForm() {
var email = document.getElementById("email").value;
function validateForm() {
...
var email2 = document.getElementById("email2").value;
http://www.uow.edu.au/~dong/w3/example/js/validation5.html
Ask user a simple math problem, only submit the form if user answers correctly
function validateForm() {
...
var answer = prompt("What is 1+2 ?");
if(answer == null || answer != 3){
return false;
}
...
Form validation
Example 5:
http://www.uow.edu.au/~dong/w3/example/js/validation5.html
Ask user a simple math problem, only submit the form if user answers correctly
Math.floor(x):
returns the greatest integer below x
for example, Math.floor(4.6) = 4
.753 * 10 = 7.53
Math.floor(7.53) = 7
http://www.uow.edu.au/~dong/w3/example/js/validation5b.html
Generate random question
function validateForm() {
...
var x = Math.floor(Math.random() * 10) + 1;
var y = Math.floor(Math.random() * 10) + 1;
var correctAnswer = x + y;
<html>
<head></head>
<body>
<form action="myService" method="get">
Email:
<input type="text“ id="email" name="email"
title="Email should not be empty"
placeholder = "error if you see me!"
oninvalid = "this.setCustomValidity( 'hello already tell u see me is error, still submit!');"
required>
<input type="submit" onsubmit="return validateForm()">
</form>
</body>
</html>
Regular expression
A regular expression describes a pattern of characters
/pattern/
[abc] Only a, b, or c
[^abc] Not a, b, nor c
[a-z] Characters a to z
[0-9] Numbers 0 to 9
\d Any Digit
\D Any Non-digit character
\s Any Whitespace
\S Any Non-whitespace character
Regular expression
A regular expression describes a pattern of characters
/pattern/
. Any Character
\. Period
{m} m Repetitions
{m,n} m to n Repetitions
. Any Character
\. Period
. \ / + * ? ^ [ ] $ ( ) { } |
Regular expression
Example 1:
<form action="myService" method="get">
</form>
http://www.uow.edu.au/~dong/w3/example/js/regex1.html
Regular expression
We can use javascript to check regular expression
<script>
function validateForm(){
var input = document.getElementById("t1").value;
if(/^[A-Z][a-z], [A-Z][a-z]$/.test(input) == false){
alert("input does not match the pattern");
return false;
}
return true;
}
</script>
<script>
function validateForm(){
var input = document.getElementById("t1").value;
if(/^[A-Z][a-z], [A-Z][a-z]$/.test(input) == false){
alert("input does not match the pattern");
return false;
}
return true;
}
</script>
</form>
http://www.uow.edu.au/~dong/w3/example/js/regex2.html
Regular expression
Example 3:
<form action="myService" method="get">
</form>
http://www.uow.edu.au/~dong/w3/example/js/regex3.html
Regular expression
Example 4:
pattern="^[0-9]{2,}$"
pattern="^\d{2,}$"
Regular expression
Example 5:
pattern="^.{5,}$"
5 or more characters
Example 6:
pattern="^.{4,10}$"
4 to 10 characters
Example 7:
pattern="^[0-9]{1,3}\.[0-9]{1,3}$"
pattern="^[^0-9][0-9]$"
pattern="^\D\d$"
Example 9:
pattern="^[0-9]+[a-z]*$"
Example 10:
pattern="^http(s?):\/\/.+$"
pattern="^[0-9]{4}-[0-9]{4}$"
Example 12:
pattern="^[0-9]{2}:[0-9]{2}:[0-9]{4}$"
Example 13:
pattern="^(NSW|ACT|NT|QLD|SA|TAS|VIC|WA) [0-9]{4}$"
pattern="^(0[1-9]|1[0-2])-20(1[0-9]|2[0-5])$"
http://www.w3schools.com/js
http://www.w3schools.com/jsref/jsref_obj_regexp.asp
document.getElementById("fee").value = "230.50";
var s = "100";
var x = Number(s);
document.getElementById("mark").innerHTML = "90";
document.getElementById("mark").innerHTML = "<i>90</i>";
Review
To get a random (decimal) number from 0 (inclusive) to 1 (exclusive):
http://www.uow.edu.au/~dong/w3/example/js/changeCircle1.html
http://www.uow.edu.au/~dong/w3/example/js/changeCircle1.html
Event
First, I need to create two images:
- one white circle circle1.png, and
- one black circle circle2.png.
<script>
function changeCircleImage() {
var image = document.getElementById("circle");
if (image.src.includes("circle1")) {
image.src = "images/circle2.png";
} else {
image.src = "images/circle1.png";
}
}
</script>
http://www.uow.edu.au/~dong/w3/example/js/changeCircle1.html
Event
Another solution:
http://www.uow.edu.au/~dong/w3/example/js/changeCircle2.html
<script>
var imageNumber = 1;
function changeCircleImage() {
if(imageNumber == 1){
imageNumber = 2;
}else{
imageNumber = 1;
}
if (imageNumber == 1) {
image.src = "images/circle1.png";
} else {
image.src = "images/circle2.png";
}
}
</script>
Event
function goToUOW() {
window.location.assign("http://www.uow.edu.au");
}
http://www.uow.edu.au/~dong/w3/example/js/eventOnClick.htm
l
Event
When the user leaves the input field, a function is triggered which transforms the
input text to uppercase:
function uppercase(){
var e = document.getElementById("discountCode");
e.value = e.value.toUpperCase();
}
http://www.uow.edu.au/~dong/w3/example/js/eventOnChange.ht
ml
Event
function mouseDown() {
document.getElementById("demo").innerHTML = "Release Me";
}
function mouseUp() {
document.getElementById("demo").innerHTML = "Thank You";
}
http://www.uow.edu.au/~dong/w3/example/js/eventOnMouseUpMouseDown.html
Event
function mouseOver() {
document.getElementById("demo").innerHTML = "Thank You"
}
function mouseOut() {
document.getElementById("demo").innerHTML = "Mouse Over Me"
}
http://www.uow.edu.au/~dong/w3/example/js/eventOnMouseOverMouseOut.html
Dynamic content
<button onClick="addSubject()">
Click here to add subject
</button>
<div id="subjectList">
</div>
function addSubject(){
//ask user for a subject code
var subject = prompt("Enter subject code");
if(subject != null){
var para = document.createElement("p");
var node = document.createTextNode(subject);
para.appendChild(node);
document.getElementById("subjectList").appendChild(para);
}
}
http://www.uow.edu.au/~dong/w3/example/js/dynamicContent1.html
Dynamic content
<button onClick="addSubject()">
Click here to add subject
</button>
<ul id="subjectList">
</ul>
function addSubject(){
//ask user for a subject code
var subject = prompt("Enter subject code");
if(subject != null){
var li = document.createElement("li");
var node = document.createTextNode(subject);
li.appendChild(node);
document.getElementById("subjectList").appendChild(li);
}
}
http://www.uow.edu.au/~dong/w3/example/js/dynamicContent2.html
Animation: setInterval
<button onClick="alertForever()">
Click here if you like alert!
</button>
function alertForever(){
//calling alertFunction for every 5000 miliseconds
var alertSchedule = setInterval(alertFunction, 5000);
}
function alertFunction() {
alert("Hello!");
}
http://www.uow.edu.au/~dong/w3/example/js/animation1.html
Animation: clearInterval
<button onclick="stopIt()">
Click here if you have enough!
</button>
function stopIt(){
clearInterval(alertSchedule);
}
function alertForever(){
//calling alertFunction for every 5000 miliseconds
alertSchedule = setInterval(alertFunction, 5000);
}
function alertFunction() {
alert("Hello!");
}
http://www.uow.edu.au/~dong/w3/example/js/animation2.html
Animation: Clock ticking
<button onclick="startClock()">
Click here to start the clock
</button>
<span id="clock"></span>
function startClock(){
//calling displayClock for every 1000 miliseconds
var clockSchedule = setInterval(displayClock, 1000);
}
function displayClock() {
document.getElementById("clock").innerHTML = new Date();
}
http://www.uow.edu.au/~dong/w3/example/js/animation3.html
Animation: Moving text
<button onclick="moveText()">
Click here to move text
</button>
function moveText() {
var e = document.getElementById("movingText");
var pos = 0;
var moveTextSchedule = setInterval(move, 50);
function move() {
pos++;
e.style.top = pos + 'px';
e.style.left = pos + 'px';
if (pos == 300) {
clearInterval(moveTextSchedule);
}
}
}
http://www.uow.edu.au/~dong/w3/example/js/animation4.html
Animation: Slide show
function slideShow() {
var imageSchedule = setInterval(changeImage, 2000);
}
function changeImage() {
var imageList = ["images/simpson2.png", "images/simpson4.png",
"images/simpson6.png", "images/simpson1.jpg", "images/simpson3.jpg",
"images/simpson5.png", "images/simpson7.jpg"];
<script>
slideShow();
</script>
http://www.uow.edu.au/~dong/w3/example/js/animation5.html
References
http://www.w3schools.com/js
● <cust>56A</cust>
● <prod>72Q</prod>
● <qty>26</qty>
● <price>7.85</price>
XML
● Using XML Document Type Definition (DTD), or XML Schema Definition (XSD),
different parties can agree on a standard XML format for interchanging data.
{
"firstName": "John",
"lastName": "Smith",
"email": "jsmith@gmail.com",
"mobile": "0211223344"
}
● In most web applications, XML and JSON are used to store or transport data,
while HTML and XSLT are used to transform and display the data.
XML:
The first example of XML:
● An XML document must contain one root element that is the parent of all
other elements
<rootElement>
<child>
<subchild>.....</subchild>
</child>
</rootElement>
XML: root element
This is NOT a well-formed XML document because it has no root element
</tag>
<student_list>
...
</student_list>
or
<studentList>
...
</studentList>
XML: attribute
<tag attribute1="..." attribute2="...">
</tag>
<dailyTransaction date='24/02/2015'>
<person staffDbId="103" operation="update">
<firstName>John</firstName>
<lastName>Smith</lastName>
<mobile>0211223344</mobile>
</person>
</dailyTransaction>
XML: relationship between elements
<parent>
<child>
<subchild>.....</subchild>
</child>
</parent>
● An XML tree starts at a root element and branches from the root to child
elements.
● The terms parent, child, and sibling are used to describe the relationships
between elements.
○ Parent have children. Children have parents.
○ Siblings are children on the same level
XML: attribute vs child element
Any attribute can be defined as a child element.
Metadata (data about data) should be stored as attributes, and the data itself
should be stored as elements.
<person gender="M">
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>jsmith@gmail.com</email>
</person>
<person>
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>jsmith@gmail.com</email>
<gender>M</gender> this is better
</person>
XML: attribute vs child element
Any attribute can be defined as a child element, so when should we use attribute
and when should we use element?
Metadata (data about data) should be stored as attributes, and the data itself
should be stored as elements.
<person>
<firstName>John</firstName>
<lastName>Smith</lastName>
<mobile>0211223344</mobile>
<staffDbId>103</staffDbId>
<operation>update</operation> this is better
</person>
XML: empty element and self-closing tag
In HTML, some elements might work well, even with a missing closing tag:
<br>
<hr>
<p>
<input ...>
<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>jsmith@gmail.com</email>
</student>
XML: entity reference
If we place a character like < inside an XML element, it will generate an error.
In this case, we need to use the entity reference <
Entity references
● Using a DTD, different parties can agree on a standard XML format for
interchanging data.
The DTD can be declared inside the XML file, or it can be defined in a separate
file:
● Internal DTD
● External DTD
DTD: internal DTD
An element is everything from the element's start tag to the element's end tag:
<firstName>John</firstName>
<lastName>Smith</lastName>
Example:
<!ELEMENT studentList (student*)>
● elementName specifies the name of the element to which the attribute applies,
● is required
<!ATTLIST elementName attributeName attributeType #REQUIRED>
● is implied: if the attribute has no default value, has no fixed value, and is not
required, then it must be declared as implied
<!ATTLIST elementName attributeName attributeType #IMPLIED>
DTD: Attribute declaration
<?xml version="1.0" ?>
<dailyTransaction date="24/02/2015">
<person staffDbId="103" operation="update">
<firstName>John</firstName>
<lastName>Smith</lastName>
<mobile>0211223344</mobile>
</person>
<person staffDbId="-1" operation="add">
<firstName>Mary</firstName>
<lastName>Jane</lastName>
<mobile>0244556677</mobile>
</person>
</dailyTransaction>
CDATA = unparsed character
data which may contain
unescaped character
● XML Schema Definition (XSD) is another way to define the legal building
blocks of an XML document. It defines the document structure with a list of
legal elements and attributes.
● Using a XSD, different parties can agree on a standard XML format for
interchanging data.
<xsd:element name="student">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
<xsd:element name="email" type="xsd:string"/>
<xsd:element name="mobile" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
XSD: student example
XML file:
<?xml version="1.0" ?>
<student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="student.xsd">
<firstName>John</firstName>
<lastName>Smith</lastName> elements and data types used in the schema
<email>jsmith@gmail.com</email> come from the namespace
<mobile>0211223344</mobile> http://www.w3.org/2001/XMLSchema
</student>
<xsd:element name="student">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
<xsd:element name="email" type="xsd:string"/>
<xsd:element name="mobile" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
XSD: student example
XML file:
<?xml version="1.0" ?>
<student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="student.xsd">
<firstName>John</firstName>
<lastName>Smith</lastName> the elements and data types that come from
<email>jsmith@gmail.com</email> the namespace
<mobile>0211223344</mobile> http://www.w3.org/2001/XMLSchema
</student> should be prefixed with xsd
<xsd:element name="student">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
<xsd:element name="email" type="xsd:string"/>
<xsd:element name="mobile" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
XSD: student example
XML file:
<?xml version="1.0" ?>
<student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="student.xsd">
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>jsmith@gmail.com</email>
<mobile>0211223344</mobile>
</student>
<xsd:element name="student">
Complex type <xsd:complexType>
<xsd:sequence>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
Simple type <xsd:element name="email" type="xsd:string"/>
<xsd:element name="mobile" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
XSD: element
<result>
<mark>85</mark>
<grade>A</grade>
</result>
<xsd:element name="result">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="mark" type="xsd:integer"/>
<xsd:element name="grade" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
XSD: complex type containing element and attribute
● Element contains other elements and attributes → complexType
<scan schedule="hourly">
<start>2018-06-20T13:00:00</start>
<finish>2018-06-20T13:01:47</finish>
<virusFound>true</virusFound>
</scan>
The attribute declarations
must always come last
<xsd:element name="scan">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="start" type="xsd:dateTime" />
<xsd:element name="finish" type="xsd:dateTime" />
<xsd:element name="virusFound" type="xsd:boolean" />
</xsd:sequence>
<xsd:attribute name="schedule" type="xsd:string" />
</xsd:complexType>
</xsd:element>
XSD: complex type containing attributes only
<price promotionCode="FAMILYDEAL">39.50</price>
<xsd:element name="price">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:decimal">
<xsd:attribute name="promotionCode" type="xsd:string" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
XSD: simple type containing no element, no attribute
● Element contains no elements, no attributes → simpleType
<website>http://www.uow.edu.au/student</website>
<lastDayToEnrol>2000-03-24</lastDayToEnrol>
<favouriteColor>blue</favouriteColor>
<grade>B</grade>
Without restriction:
<xsd:element name="grade" type="xsd:string" />
With restriction:
<xsd:element name="grade">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="A"/>
<xsd:enumeration value="B"/>
<xsd:enumeration value="C"/>
<xsd:enumeration value="D"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
XSD: simple type with restriction
<mark>84</mark>
Without restriction:
<xsd:element name="mark" type="xsd:integer" />
With restriction:
<xsd:element name="mark">
<xsd:simpleType>
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="100"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<?xml version="1.0" ?>
XSD: studentList example
<studentList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="studentList.xsd">
<student>
<firstName>John</firstName>
<lastName>Smith</lastName>
<email>jsmith@gmail.com</email>
</student>
<student>
<firstName>Mary</firstName>
<lastName>Jane</lastName>
<email>mjane@gmail.com</email>
</student>
</studentList>
<xsd:element name="dailyTransaction">
<xsd:complexType>
<xsd:sequence>
...
</xsd:sequence>
<xsd:attribute name="date" type="xsd:string" />
</xsd:complexType>
</xsd:element>
XSD: dailyTransaction example
<dailyTransaction date="24/02/2015">
<person staffDbId="103" operation="update">
...
</person>
<person staffDbId="-1" operation="add">
...
</person>
</dailyTransaction>
<xsd:element name="dailyTransaction">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="person" minOccurs="0" maxOccurs="unbounded">
...
</xsd:element>
</xsd:sequence>
<xsd:attribute name="date" type="xsd:string" />
</xsd:complexType>
</xsd:element>
XSD: dailyTransaction example
<person staffDbId="103" operation="update">
<firstName>John</firstName>
<lastName>Smith</lastName>
<mobile>0211223344</mobile>
</person>
XSLT
Joseph Tonien
XSL
used to transform XML file into other file formats, such as HTML
</xsl:stylesheet>
XSLT
Have a look at the XML file
http://www.uow.edu.au/~dong/w3/example/XSLT/transaction.xml
http://www.uow.edu.au/~dong/w3/example/XSLT/transaction1.xml
XSLT
The content of transaction1.xml is exactly the same as transaction.xml,
except that it uses an xml stylesheet: transaction-style1.xsl
<xsl:template match="/dailyTransaction">
<html>
<head>
<title>XSLT example</title>
</head>
<body>
<h1>Daily transaction <xsl:value-of select="@date" /> </h1>
<ul>
<xsl:for-each select="person">
<li>
<xsl:value-of select="firstName" />
<xsl:text> </xsl:text>
<xsl:value-of select="lastName" />
<xsl:text>, </xsl:text>
<xsl:value-of select="mobile" />
<xsl:text>, </xsl:text>
<xsl:value-of select="@staffDbId" />
<xsl:text>, </xsl:text>
<xsl:value-of select="@operation" />
</li>
</xsl:for-each>
</ul>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XSLT
Let’s look at the xml stylesheet: transaction-style1.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:template match="/dailyTransaction">
<html>
<head>
<title>XSLT example</title>
</head>
<body>
<h1>Daily transaction <xsl:value-of select="@date" /> </h1>
<ul>
<xsl:for-each select="person">
<li>
<xsl:value-of select="firstName" />
<xsl:text> </xsl:text>
<xsl:value-of select="lastName" />
<xsl:text>, </xsl:text>
<xsl:value-of select="mobile" />
<xsl:text>, </xsl:text>
<xsl:value-of select="@staffDbId" />
<xsl:text>, </xsl:text>
<xsl:value-of select="@operation" />
</li>
</xsl:for-each>
</ul>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XSLT
Let’s look at the xml stylesheet: transaction-style1.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:template match="/dailyTransaction">
<html>
<head>
<title>XSLT example</title>
</head>
<body>
<h1>Daily transaction <xsl:value-of select="@date" /> </h1>
<ul>
<xsl:for-each select="person">
<li>
<xsl:value-of select="firstName" />
<xsl:text> </xsl:text>
<xsl:value-of select="lastName" />
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:text>, </xsl:text>
<dailyTransaction date="24/02/2015">
<xsl:value-of select="mobile" /> <person staffDbId="103" operation="update">
<xsl:text>, </xsl:text> <firstName>John</firstName>
<lastName>Smith</lastName>
<xsl:value-of select="@staffDbId" /> <mobile>0211223344</mobile>
<xsl:text>, </xsl:text> </person>
<person staffDbId="-1" operation="add">
<xsl:value-of select="@operation" /> <firstName>Mary</firstName>
</li> <lastName>Jane</lastName>
<mobile>0244556677</mobile>
</xsl:for-each> </person>
</ul> ...
</body> </dailyTransaction>
</html>
</xsl:template>
</xsl:stylesheet>
XSLT
Let’s look at the xml stylesheet: transaction-style1.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:template match="/dailyTransaction">
<html>
<head>
<title>XSLT example</title>
</head>
<body>
<h1>Daily transaction <xsl:value-of select="@date" /> </h1>
<ul>
<xsl:for-each select="person">
<li>
<xsl:value-of select="firstName" />
<xsl:text> </xsl:text>
<xsl:value-of select="lastName" />
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:text>, </xsl:text>
<dailyTransaction date="24/02/2015">
<xsl:value-of select="mobile" /> <person staffDbId="103" operation="update">
<xsl:text>, </xsl:text> <firstName>John</firstName>
<lastName>Smith</lastName>
<xsl:value-of select="@staffDbId" /> <mobile>0211223344</mobile>
<xsl:text>, </xsl:text> </person>
<person staffDbId="-1" operation="add">
<xsl:value-of select="@operation" /> <firstName>Mary</firstName>
</li> <lastName>Jane</lastName>
<mobile>0244556677</mobile>
</xsl:for-each> </person>
</ul> ...
</body> </dailyTransaction>
</html>
</xsl:template>
</xsl:stylesheet>
XSLT
Let’s look at the xml stylesheet: transaction-style1.xsl
select="@date"
means the attribute
<?xml version="1.0" encoding="UTF-8" ?>
<dailyTransaction date="24/02/2015">
<person staffDbId="103" operation="update">
<firstName>John</firstName>
<lastName>Smith</lastName>
<mobile>0211223344</mobile>
</person>
<person staffDbId="-1" operation="add">
<firstName>Mary</firstName>
<lastName>Jane</lastName>
<mobile>0244556677</mobile>
</person>
...
</dailyTransaction>
XSLT
Let’s look at the xml stylesheet: transaction-style1.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:template match="/dailyTransaction">
<html>
<head>
<title>XSLT example</title>
</head>
<body>
<h1>Daily transaction <xsl:value-of select="@date" /> </h1>
<ul>
<xsl:for-each select="person">
<li>
<xsl:value-of select="firstName" />
<xsl:text> </xsl:text>
<xsl:value-of select="lastName" />
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:text>, </xsl:text> <dailyTransaction date="24/02/2015">
<xsl:value-of select="mobile" /> <person staffDbId="103" operation="update">
<xsl:text>, </xsl:text> <firstName>John</firstName>
<xsl:value-of select="@staffDbId" /> <lastName>Smith</lastName>
<mobile>0211223344</mobile>
<xsl:text>, </xsl:text> </person>
<xsl:value-of select="@operation" /> <person staffDbId="-1" operation="add">
</li> <firstName>Mary</firstName>
</xsl:for-each>
<lastName>Jane</lastName>
<mobile>0244556677</mobile>
</person>
</ul> ...
</body> </dailyTransaction>
</html>
</xsl:template>
</xsl:stylesheet>
XSLT
Let’s look at the xml stylesheet: transaction-style1.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:template match="/dailyTransaction">
<html>
<head>
<title>XSLT example</title>
</head>
<body>
<h1>Daily transaction <xsl:value-of select="@date" /> </h1>
<ul>
<xsl:for-each select="person">
<li>
<xsl:value-of select="firstName" />
<xsl:text> </xsl:text>
<xsl:value-of select="lastName" /> <?xml version="1.0" encoding="UTF-8" ?>
<xsl:text>, </xsl:text> <dailyTransaction date="24/02/2015">
</xsl:for-each>
<lastName>Jane</lastName>
<mobile>0244556677</mobile>
</person>
</ul> ...
</body> </dailyTransaction>
</html>
</xsl:template>
XSLT
<ul>
<xsl:for-each select="person">
<li>
<xsl:value-of select="firstName" />
<xsl:text> </xsl:text>
<xsl:value-of select="lastName" />
<xsl:text>, </xsl:text>
<xsl:value-of select="mobile" />
<xsl:text>, </xsl:text>
<xsl:value-of select="@staffDbId" />
<xsl:text>, </xsl:text>
<xsl:value-of select="@operation" />
</li>
</xsl:for-each>
</ul>
http://www.uow.edu.au/~dong/w3/example/XSLT/transaction2.xml
XSLT
View the source of the xml stylesheet: transaction-style2.xsl
we can see that it displays a table
<xsl:for-each select="person">
<xsl:sort select="lastName"/>
<tr>
...
</tr>
</xsl:for-each>
XSLT
Example 3: Compare transaction-style2.xsl with transaction-style3.xsl
<table border="1">
<tr>
<th>Name</th>
<th>Mobile</th>
<th>Staff Id</th>
<th>Operation</th>
</tr>
<xsl:apply-templates select="person">
<xsl:sort select="lastName" />
</xsl:apply-templates>
</table>
https://www.w3schoo
ls.com/xml/xsl_apply
_templates.asp
XSLT
Now compare transaction-style2.xsl with transaction-style3.xsl
<xsl:template match="person">
<tr>
<td>
<xsl:value-of select="firstName" />
<xsl:text> </xsl:text>
<xsl:value-of select="lastName" />
</td>
<td>
<xsl:value-of select="mobile" />
</td>
<td>
<xsl:value-of select="@staffDbId" />
</td>
<td>
<xsl:value-of select="@operation" />
</td>
</tr>
</xsl:template>
XSLT
Example 4: Now look at transaction4.xml
http://www.uow.edu.au/~dong/w3/example/XSLT/transaction4.xml
XSLT
Now look at transaction-style4.xsl
<xsl:choose>
<xsl:when test="@operation = 'remove'">
<td bgcolor="#ffe6e6">
<xsl:value-of select="@operation" />
</td>
</xsl:when>
<xsl:when test="@operation = 'add'">
<td bgcolor="#ffffe6">
<xsl:value-of select="@operation" />
</td>
</xsl:when>
<xsl:otherwise>
<td bgcolor="#d6f5d6">
<xsl:value-of select="@operation" />
</td>
</xsl:otherwise>
</xsl:choose>
XSLT
Example 5: Now look at transaction5.xml
http://www.uow.edu.au/~dong/w3/example/XSLT/transaction5.xml
XSLT
Now look at transaction-style5.xsl
<xsl:choose>
<xsl:otherwise>
<td> <xsl:value-of select="mobile" /> </td>
</xsl:otherwise>
</xsl:choose>
XSLT
Example 6: Now look at transaction6.xml
http://www.uow.edu.au/~dong/w3/example/XSLT/transaction6.xml
XSLT
Now look at transaction-style6.xsl
<xsl:for-each select="person[@operation='add']">
<xsl:sort select="lastName"/>
<tr>
<td>
<xsl:value-of select="firstName" />
<xsl:text> </xsl:text>
<xsl:value-of select="lastName" />
</td>
<td>
<xsl:value-of select="mobile" />
</td>
<td>
<xsl:value-of select="@staffDbId" />
</td>
<td>
<xsl:value-of select="@operation" />
</td>
</tr>
</xsl:for-each>
XSLT
Example 7: Now look at transaction7.xml
http://www.uow.edu.au/~dong/w3/example/XSLT/transaction7.xml
XSLT
Now look at transaction-style7.xsl
...
<td>
<xsl:value-of select="@staffDbId" />
</td>
<td align="center">
<img>
<xsl:attribute name="src">
<xsl:text>images/</xsl:text>
<xsl:value-of select="@operation"/>
<xsl:text>.png</xsl:text>
</xsl:attribute>
<xsl:attribute name="width">
<xsl:text>30px</xsl:text>
</xsl:attribute>
</img>
</td>
...
XSLT
Example 8: Now look at transaction8.xml
http://www.uow.edu.au/~dong/w3/example/XSLT/transaction8.xml
XSLT
Now look at transaction-style8.xsl
...
<td>
<xsl:choose>
<xsl:when test="number(@staffDbId) < 0">
<span style="color:red">
<xsl:value-of select="@staffDbId" />
</span>
</xsl:when>
<xsl:otherwise>
<span style="color:green">
<xsl:value-of select="@staffDbId" />
</span>
</xsl:otherwise>
</xsl:choose>
</td>
...
References
http://www.w3schools.com/xsl
JSON
Joseph Tonien
JavaScript Object Notation (JSON)
{
"firstName":"John",
"lastName":"Smith",
"domestic":true,
"fee":100.5
}
JSON
{
"firstName":"John",
"lastName":"Smith",
"domestic":true,
"fee":100.5
}
JSON
objJSON = JSON.stringify(obj);
obj = JSON.parse(objJSON);
JSON
Example 1: http://www.uow.edu.au/~dong/w3/example/json/example1.html
function showJSON(){
//create a student object
var john = {};
john.firstName = "John";
john.lastName = "Smith";
john.domestic = true;
john.fee = 100.50;
<button onClick="showJSON()">
Click here to see JSON string
</button>
JSON
Example 2: http://www.uow.edu.au/~dong/w3/example/json/example2.html
function showObject(){
//JSON string
var johnJSON = '{"firstName":"John","lastName":"Smith",
"domestic":true,"fee":100.5}';
<button onClick="showObject()">
Click here to see object from JSON
</button>
JSON
[
{
"firstName":"John",
"lastName":"Smith"
},
{
"firstName":"Kate",
"lastName":"Williams"
}
]
JSON
Example 3: http://www.uow.edu.au/~dong/w3/example/json/example3.html
function showJSON(){
var john = {};
john.firstName = "John";
john.lastName = "Smith";
function showArray(){
//JSON string
var studentListJSON = '[{"firstName":"John","lastName":"Smith"},
{"firstName":"Kate","lastName":"Williams"}]';
<button onClick="showArray()">
Click here to see array from JSON
</button>
JSON
Example 5: http://www.uow.edu.au/~dong/w3/example/json/example5.html
function showJSON(){
var john = {}; //create a student object
john.firstName = "John";
john.lastName = "Smith";
john.enrolledSubjects = [];
//empty array to hold subjects
{
"firstName":"John",
"lastName":"Smith",
"enrolledSubjects":[
{
"code":"MATH101",
"title":"Algebra"
},
{
"code":"CSIT122",
"title":"C programming"
}
]
}
Ajax-JSON example: Stock market
Assume that there is a JSON file, called market.json. Write HTML and
JavaScript codes that do the following:
There is a button “Click here to view Stock Market Activity”. When the user clicks on
this button, make an Ajax call to get the stock information from the JSON file and
display them in a table. http://www.uow.edu.au/~dong/w3/example/json/stock/market.html
Ajax-JSON example: Stock market
This is the content of the JSON file market.json
{
"queryTime":"24/02/2015 11:30:00",
"indexList":[
{
"name":"NASDAQ",
"value":4725.64,
"change":-37.58,
"netPercentage":0.79
},
{
"name":"NASDAQ-100 (NDX)",
"value":4312.01,
"change":-29.38,
"netPercentage":0.68
},...
]
}
Ajax-JSON example: Stock market
<button onClick="getMarketAjax()">
Click here to view Stock Market Activity
</button>
function getMarketAjax(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
processResult(xhttp);
}
};
xhttp.open("GET", "market.json", true);
xhttp.send();
}
{
Ajax-JSON example: Stock market "queryTime":"24/02/2015 11:30:00",
"indexList":[
function processResult(xhttp){ {
1 "name":"NASDAQ",
var jsonText = xhttp.responseText; "value":4725.64,
"change":-37.58,
var marketObj = JSON.parse(jsonText);
"netPercentage":0.79
display(marketObj); },...
]
} }
3
{
Ajax-JSON example: Stock market "queryTime":"24/02/2015 11:30:00",
"indexList":[
function processResult(xhttp){ {
1 "name":"NASDAQ",
var jsonText = xhttp.responseText; "value":4725.64,
"change":-37.58,
var marketObj = JSON.parse(jsonText);
"netPercentage":0.79
display(marketObj); },...
]
} }
function processResult(xhttp){ {
1 "name":"NASDAQ",
var jsonText = xhttp.responseText; "value":4725.64,
http://www.uow.edu.au/~dong/w3/example/json/stock/market.html
and
http://www.uow.edu.au/~dong/w3/example/json/stock/market2.html
References
http://www.w3schools.com/json
HTML5:
Graphic Canvas,
Drag and Drop
Joseph Tonien
School of Computing and Information Technology
University of Wollongong
HTML 5
Canvas
(0,0) x axis
There are other rendering contexts for canvas that are not
covered in this subject:
WebGLRenderingContext,
WebGL2RenderingContext
Hello World
Hello World
<button onClick="drawTextHello()">
Start
</button>
Hello World
function drawTextHello(){
// get the canvas's 2d context
// fillText
// strokeText
}
Hello World
<canvas id="canvas" width="1300" height="500"
style="border:1px solid black;">
Your browser does not support canvas.
</canvas>
// fillText
context.font = "italic small-caps bold 50px Arial";
context.fillText("Hello World", 200, 100);
// strokeText
context.font = "oblique 100px Courier New";
context.strokeText("Hello World", 250, 300);
Stroke Demo 1
Stroke Demo 1
<button onClick="strokeDemo()">
Start
</button>
Stroke Demo 1
function strokeDemo(){
// get the canvas's 2d context
Y
Stroke Demo 1
context.beginPath();
context.moveTo(50, 10);
context.lineTo(50, 400);
context.lineTo(250, 400);
context.closePath();
Fill Demo 1
Fill Demo 1
function fillDemo(){
// get the canvas's 2d context
context.beginPath();
context.moveTo(50, 10);
context.lineTo(50, 400);
context.lineTo(250, 400);
context.closePath();
context.fillStyle="#F5FFFA";
context.fill();
Fill Demo 2
context.fillStyle="#F5FFFA";
context.fill();
UOW 1
UOW 1
4. letter W
3. letter O (inner) filled with black
filled with white
1. letter U
filled with black 2. letter O (outer)
filled with black
UOW 1
<button onClick="drawUOW()">
Start
UOW 1
function drawUOW(){
// get the canvas's 2d context
// letter U
// letter O (outer)
// letter O (inner)
// letter W
}
UOW 1 // letter U
context.beginPath();
context.moveTo(0, 0);
context.lineTo(0, 350);
context.lineTo(50, 400);
context.lineTo(250, 400);
context.lineTo(300, 350);
context.lineTo(300, 0);
context.lineTo(200, 0);
context.lineTo(200, 280);
context.lineTo(180, 300);
context.lineTo(120, 300);
context.lineTo(100, 280);
context.lineTo(100, 0);
context.closePath();
UOW 1 // letter U
context.beginPath();
context.moveTo(0, 0);
...
context.lineTo(100, 0);
context.closePath();
context.fillStyle="black";
context.fill();
context.strokeStyle="blue";
context.lineWidth = "4";
context.stroke();
UOW 2 - using object
UOW 2 // letter U
context.beginPath();
context.moveTo(0, 0); Positions:
... is an array of
coordinates
context.lineTo(100, 0);
context.closePath();
context.fillStyle="black";
context.fill();
context.strokeStyle="blue";
context.lineWidth = "4";
context.stroke();
● Positions
● Fill style
Using object to store the letter setting:
● Stroke style
● Line width
UOW 2 // letter U
var letterU = {
positions: [ [0, 0], …, [100, 0] ],
fillStyle: "black",
strokeStyle: "blue",
lineWidth: "4"
};
● Positions
● Fill style
Using object to store the letter setting:
● Stroke style
● Line width
UOW 2 // letter U
var letterU = {
positions: [ [0, 0], …, [100, 0] ],
fillStyle: "black",
strokeStyle: "blue",
lineWidth: "4"
};
// letter O outer
// letter O inner
// letter W
● Positions
● Fill style
Using object to store the letter setting:
● Stroke style
● Line width
UOW 2
function drawUOW(){
// objects contains letter's drawing setting
// letter U object
// letter O outer object
// letter O inner object
// letter W object
// array of letter settings
// fill
// stroke
}
UOW 2 // letter U
var letterU = {
context.moveTo(firstPosition[0], firstPosition[1]);
context.lineTo(position[0], position[1]);
}
// finally close the path
context.closePath();
UOW 2 // letter U
var letterU = {
positions: [ [0, 0], …, [100, 0] ],
context.fill(); };
// stroke
context.strokeStyle = letter.strokeStyle;
context.lineWidth = letter.lineWidth;
context.stroke();
Move the Dog
Move the Dog
<button onClick="move()">
Move to
</button>
X:<input id="x" value="200"/>
Y:<input id="y" value="100"/>
function move(){
// get the canvas's 2d context
//dataTransfer object
event.dataTransfer.setData("dragId", dragId);
}
Drag and Drop
}
Drag and Drop
https://developer.mozilla.org/en-
US/docs/Web/API/HTML_Drag_and_Drop_API/Drag_operations#droptargets
A listener for the dragEnter and dragOver events are used to indicate valid drop
targets.
Most areas of a web page are not valid places to drop data.
Thus, the default handling of these events is not to allow a drop.
If you want to allow a drop, you must prevent the default handling by cancelling
the event. Calling the preventDefault() method during both a dragEnter and
dragOver event will indicate that a drop is allowed at that location.
Drag and Drop
https://developer.mozilla.org/en-US/docs/Web/Events/dragenter
Calling the preventDefault() method during a dragOver event will indicate that a
drop is allowed at that location.
function dragOver(event){
event.preventDefault();
}
Drag and Drop: Hello World
droppable elements:
elements that can be
dropped on
Drag and Drop: Hello World
draggable elements:
elements that we can drag
droppable elements:
elements that can be
dropped on
. . .
Drag and Drop: Hello World
<span id="hello" draggable="true"
onDragStart="dragStart(event)" >hello</span>
event.dataTransfer.setData("dragId", dragId);
}
Drag and Drop: Hello World
<span id="hello" draggable="true"
onDragStart="dragStart(event)" >hello</span>
event.dataTransfer.setData("dragId", dragId);
}
Drag and Drop: Hello World
}
Drag and Drop: Hello World
Calling the preventDefault() method during a dragOver event will indicate that a
drop is allowed at that location.
function dragOver(event){
event.preventDefault();
}
Cat, Dog, and Fish 1
droppable elements:
elements that can be
dropped on
draggable elements:
elements that we can be
dragged
Cat, Dog, and Fish 1 DRAGGABLE ELEMENTS
function dragStart(event){
event.dataTransfer.setData("dragId", dragId);
}
Cat, Dog, and Fish 1 DROPPABLE ELEMENTS
function drop(event){
}
Cat, Dog, and Fish 1 DROPPABLE ELEMENTS
/*
*/
function dragOver(event){
event.preventDefault();
}
Cat, Dog, and Fish 2
droppable elements:
elements that can be
dropped on
draggable elements:
elements that we can be
dragged
Cat, Dog, and Fish 2
DRAGGABLE ELEMENTS
function drop(event){
// get the drop element ID
}
The only difference between CAT-DOG-FISH (1)
Cat, Dog, and Fish 2 and CAT-DOG-FISH (2) is the implementation of
the function drop(event)
droppable elements:
elements that can be
dropped on
draggable elements:
elements that we can be
dragged
The only difference between CAT-DOG-FISH (2) and CAT-DOG-FISH (3) is the
implementation of the function drop(event)
Cat, Dog, and Fish 3
The only difference between CAT-DOG-FISH (2) and CAT-DOG-FISH (3) is the
implementation of the function drop(event)
function drop(event){
// get the drop element ID
}
Cat, Dog, and Fish 3
The only difference between CAT-DOG-FISH (2) and CAT-DOG-FISH (3) is the
implementation of the function drop(event)
// get the drop element ID
var dropId = event.target.id;
● https://www.w3schools.com/html/html5_canvas.asp
● https://developer.mozilla.org/en-
US/docs/Web/API/Canvas_API/Tutorial
● https://www.w3schools.com/html/html5_draganddrop.asp
● https://developer.mozilla.org/en-
US/docs/Web/API/HTML_Drag_and_Drop_API