N341 Home Work 1 HTML/JavaScript |
Modified: |
Essentials and Downloads
- Zone.Identifier files are created by IE can all be deleted.
Overview
The purpose of the first homework is to introduce some of the basic concepts of HTML and JavaScript.
HTML limits user interaction to a mostly static user interface; a scripting language adds dynamic interaction through programming to that possible in HTML alone.
There are two approaches that use client-side scripting for user interaction: 1) define the user interface in HTML but change elements displayed as the user interacts, and 2) generate new HTML as the user interacts.
The following assignments are designed to practice interacting with the user using both HTML and the JavaScript scripting language.
Assignment - 3 parts
0. Assignment 2 below uses photos of our class. We'll need names to match photos so you can help by matching your own now.
1. HTML - Design two Web pages, each should fit completely on one screen.
It should be hand created and minimally display:
2. JavaScript

Implement a dynamic user interface that turns a light ON or OFF by
displaying the images at right.
Some hints:
| <img src="on.png" /> |
The following uses the non-displaying span element to name the tag "light".
| <span id="light"> <img src="on.png" /> </span> |
| <script
language="JavaScript"> light.innerHTML = '<img src="off.png" />' ; </script> |
When executed 3 or more times the OFF image is displayed.
<span id="light"> <img src="on.png" /> </span>
<form name="Example0">
<INPUT type="button" value="Change" name="Switch" onClick="flip()" />
</form>
<script language="JavaScript">
var times = 0;
function flip() {
alert("Change button clicked:" + times);
document.Example0.Switch.value = "Change " + times;
times = times + 1;
if(times > 3)
light.innerHTML = '<img src="off.png" />' ;
else
light.innerHTML = '<img src="on.png" />' ;
}
</script>
|
Note that in the above JavaScript, times is defined outside any function so is a global variable.
Suggested Start for B and C
- The assignment uses photos and names of our class. We need names to match photos but you'll need to help by matching your own.
- Click here to list class student names and photos. This file should be ready the day following the first class.
- Find yours and check the button that matches your name and photo.
- Click the Match ME button.
- Download the class photo ZIP file and extract the photo files to a directory named \N341\HW1.
- Put all files in HW1 subdirectory.
- Download class names, emails and photos in semicolon separated fields:
- Copy from the browser screen, paste and save to HW1.txt
Data services
Data services can provide dynamic data from over the Web (Web services) such as weather radar images or static data from files.
In this assignment, we will create a JavaScript photo gallery from the class photo information stored as a text file on your computer.
AJAX (asynchronous JavaScript and XML) is commonly used to describe the techniques employed in the assignment, though for simplification, we are using a semicolon delimited text file for data rather than than XML.
The data is organized for each person as: name; photo; email each separated by ; to allow splitting into a string array.
Shato, William Paul;1.jpg;wshato@ius.edu;
Wisman, Raymond F.;2.jpg;rwisman@ius.edu;
Abourabine, Kristin M;3.jpg;kabourab@ius.edu;
Adams, Vanessa Rene;4.jpg;vadams@ius.edu;
Apple, William;5.jpg;wapple@indiana.edu
The following examples work through the concepts and techniques needed to implement the assignment.
Dynamic User Interface
HTML is static but can be changed dynamically by JavaScript.
The following JavaScript displays the form:
Display the photo: <span id="display">Your picture</span> here.
<script type=text/javascript>
function showPhoto() {
display.innerHTML = "<img src='2.jpg'/>";
}
</script>
<form name="Example1">
<input type=button value="Show" onclick="showPhoto()"/>
</form>
Display the photo: Your picture here. Clicking Show would insert the HTML code to display an image.
<span id="display">Your picture</span> here.
is changed by:
display.innerHTML = "<img src='2.jpg'/>";
to:
<span id="display"><img src='2.jpg'/></span> here.
Example 1
Notice that:
Always put some text there, otherwise, it is an error in some cases.
Example 1<br/>
Display the photo: <span id="display">Your picture</span> here.
<script type=text/javascript>
function showPhoto() {
display.innerHTML = "<img src='2.jpg'/>";
}
</script>
<form name="Example1">
<input type=button value="Show" onclick="showPhoto()"/>
</form>Display the photo:
here.
InnerHTML
The two parts that dynamically changes what the user sees is:
<span id="display">Your picture</span>
display.innerHTML = "<img src='2.jpg'/>"
where:
Combining Data and a Dynamic User Interface
Shato, William Paul;1.jpg;wshato@ius.edu;
Wisman, Raymond F.;2.jpg;rwisman@ius.edu;
Abourabine, Kristin M;3;kabourab@ius.edu;
Adams, Vanessa Rene;4.jpg;vadams@ius.edu;
Apple, William;5.jpg;wapple@indiana.edu
Data retrieved from a data service can be used to dynamically update the page.
The important ideas below are:
To determine the browser to create the correct HTTP request object:
| if(typeof ActiveXObject != "undefined")
// IE var req = new ActiveXObject("Microsoft.XMLHTTP"); else // Other browsers var req = new XMLHttpRequest(); |
s = "Shato, William Paul;1.jpg;wshato@ius.edu;Wisman, Raymond F.;2.jpg;rwisman@ius.edu;
..."
Wisman, Raymond F.;2.jpg;rwisman@ius.edu (the 2nd person) split in the array locations:
<img src = '2.jpg'/>
which displays the photo.
Example 2
- Save the example in the same directory with HW1.txt and photo images.
- Load the example into IE or other browser, FrontPage cannot execute.
Use the course Web server address for testing in a browser: http://iu-...
Example 2<br/> Display the photo: <span id="display">.</span> here.
<script type=text/javascript>
if(typeof ActiveXObject != "undefined") // IE
var req = new ActiveXObject("Microsoft.XMLHTTP");
else // Other browsers
var req = new XMLHttpRequest();
req.open('GET', 'hw1.txt', false);
req.send(null);
s=req.responseText;
var myArray = s.split(";");function showPhoto() {
alert( "<img src='"+myArray[4]+"'/>" );display.innerHTML="<img src='"+myArray[4]+"'/>";
}
</script>
<form name="Example2">
<input type=button value="Show" onclick="showPhoto()"/>
</form>
Display the photo: . here. HW1.txt example
Shato, William Paul;1.jpg;wshato@ius.edu;
Wisman, Raymond F.;2.jpg;rwisman@ius.edu;
Abourabine, Kristin M;3;kabourab@ius.edu;
Adams, Vanessa Rene;4.jpg;vadams@ius.edu;
Apple, William;5.jpg;wapple@indiana.edu
Forms and Parameter passing
User input is through a form text box, button, etc.
In the following form, a name can be entered and the button clicked, producing the same results as above.
Display the photo: . here.
The key change is that we now need to search the array for a string, the name entered.
We can do a linear search by iterating through the array searching for an approximate, "Ray" approximately matches "Raymond", or is a substring.
"Raymond".indexOf("mon") returns 3
"Raymond".indexOf("Fred") returns -1
If an approximate match is found, change the innerHTML to the image.
Other changes are:
Example 3
Use the course Web server address for testing in a browser: http://iu-...
Example 3<br/> Display the photo: <span id="display">.</span> here.
<script type=text/javascript>
if(typeof ActiveXObject != "undefined") // IE
var req = new ActiveXObject("Microsoft.XMLHTTP");
else // Other browsers
var req = new XMLHttpRequest();
req.open('GET', 'hw1.txt', false);
req.send(null);
s=req.responseText;
var myArray = s.split(";");function showPhoto( findName ) {
if( findName == "") return;
for(i=0; i<myArray.length; i++)
if( myArray[i].indexOf( findName ) != -1 ) {
display.innerHTML="<img src='" + myArray[i+1] + "'/>";
return;
}
}
</script>
<form name="Example3">
<input type=button value="Show" onclick="showPhoto( document.Example3.Name.value )"/>
<input type=text id="Name"/>
</form>
Example 4
Use the course Web server address for testing in a browser: http://iu-...
This example retrieves a complete list and displays each name as a hyperlink to the corresponding photo.
<a href = '2.jpg'>Wisman, Raymond F.</a>
Note the single ' inside the double ".
Example 4<br/> <span id="displayList">.</span>
<script type=text/javascript>
if(typeof ActiveXObject != "undefined") // IE
var req = new ActiveXObject("Microsoft.XMLHTTP");
else // Other browsers
var req = new XMLHttpRequest();
req.open('GET', 'hw1.txt', false);
req.send(null);
s=req.responseText;
var myArray = s.split(";");
var result="";
for(i=0; i<myArray.length; i=i+3)
result=result + "<a href='" + myArray[i+1] + "'>" + myArray[i] + "</a><br/>";
displayList.innerHTML=result;
</script>
Produces the clickable hyperlinks to photos.
Shato, William Paul
Wisman, Raymond F.
Hints
- <a href='mailto:obama@whitehouse.gov'>President</a> is HTML for sending email via a text link.
- <a href='mailto:obama@whitehouse.gov'><img src='obama.jpg'/></a> is HTML for sending email via an image link.
- myArray.length is the length or number of elements in myArray.
- Define a global variable to index myArray to the current image to display.
- Define a form in HTML with the two buttons Next and Previous.
- The Next button should execute function next() when clicked.
- <input type=button value="Next" onclick="next( )"/>
- Function next() should increment the global index and assign displayList.innerHTML the current image to display.
HTML and JavaScript
- Install Home Work 1 files for Assignment 1, 2A, 2B and 2C to course Web server for testing and grading by instructor.
- Use the course Web server address for testing in a browser: http://iu-...
- Email instructor with link to the first page of Assignment 1.
- Make the link active so that when clicked a browser will display the page. Try sending it to yourself first to make sure it works.
- Notify rwisman@ius.edu with subject: N341 Homework 1 - Your name