Download presentation
Published byHannah O’Connor’ Modified over 7 years ago
1
CMPE 280 Web UI Design and Development September 7 Class Meeting
Department of Computer Engineering San Jose State University Fall 2017 Instructor: Ron Mak
2
The Document Object Model (DOM)
Your web browser represents the contents of an HTML page internally using the Document Object Model (DOM).
3
The DOM, cont’d Several key objects of the DOM are children of the special window object. Some important children of window: Object Represents Notes document Current HTML page Most commonly scripted object location Current page URL Change location.href to move to another page history List of recently visited pages Access to view previously visited pages status Browser status bar Use to set a message in the status bar
4
Chrome and the DOM Use the Chrome browser’s Developer Tools to manually manipulate the DOM. Load any web page into the Chrome browser. View Developer Developer Tools Console tab Enter Chrome displays the valid choices in a drop-down menu after you type each period. document.body.style.backgroundColor="yellow" Note: CSS uses background-color but DOM uses backgroundColor. Demo
5
Introduction to JavaScript
Originally used only on the client side. Interpreted by the browser. Now available on the server via node.js. Open-source V8 JavaScript engine developed for Google’s Chrome browser. Syntax similar to Java. interpreted object-oriented weakly typed dynamic
6
Introduction to JavaScript, cont’d
Purposes Validate user’s form input data Provide interactivity Modify existing HTML page On the client side, you can write JavaScript code that directly manipulates the DOM.
7
Example JavaScript Code
js/backgroundcolor1.html <!DOCTYPE html> <html lang="en-US"> <head> <meta charset="UTF-8"> <title>Background Color #1</title> </head> <body> <form action=""> <fieldset> <legend>Color buttons</legend> <p> <input type="button" value="Red" onclick="document.body.style.backgroundColor='red'" /> <input type="button" value="Green" onclick="document.body.style.backgroundColor='green'" /> <input type="button" value="Blue" onclick="document.body.style.backgroundColor='blue'" /> </p> </fieldset> </form> </body> </html> Button events Event handlers Demo
8
Example JavaScript Function
<!DOCTYPE html> <html lang="en-US"> <head> <meta charset="UTF-8"> <title>Background Color #2</title> <script type="text/javascript"> function changeBGColor(color) { document.body.style.backgroundColor = color; } </script> </head> <body> <form action=""> <fieldset> <legend>Color buttons</legend> <p> <input type="button" value="Red" onclick="changeBGColor('red')" /> <input type="button" value="Green" onclick="changeBGColor('green')" /> <input type="button" value="Blue" onclick="changeBGColor('blue')" /> </p> </fieldset> </form> </body> </html> js/backgroundcolor2.html Demo
9
JavaScript Variables JavaScript variables are dynamically typed.
The type of a variable’s value is determined at run time. What is the value of sum in each example? var x = "1"; var y = "2"; var sum = x + y; var x = 1; var y = 2; var sum = x + y;
10
Obtaining Text Input Field Values
<body> <form action=""> <fieldset> <legend>User input</legend> <p> <label>First number:</label> <input id="first" type="text" /> </p> <label>Second number:</label> <input id="second" type="text" /> <input type="button" value="Add version 1" onclick="add1()" /> <input type="button" value="Add version 2" onclick="add2()" /> </fieldset> </form> </body> js/adds1.html
11
Obtaining Text Input Field Values, cont’d
<head> <meta charset="UTF-8"> <title>Add Versions #1</title> <script type="text/javascript"> function add1() { var x = document.getElementById("first").value; var y = document.getElementById("second").value; var sum = x + y; alert(x + " + " + y + " = " + sum); } function add2() var x = parseInt(document.getElementById("first").value); var y = parseInt(document.getElementById("second").value); </script> </head> js/adds1.html Demo
12
Modifying the DOM <body> <form action=""> <fieldset>
... </fieldset> </form> <div id="outputDiv"> <p>Output will appear here.</p> </div> </body> js/adds2.html
13
Modifying the DOM, cont’d
<head> ... <script type="text/javascript"> function outputSum(x, y) { var sum = x + y; document.getElementById("outputDiv").innerHTML = "<p><strong>" + x + " + " + y + " = " + sum + "</strong></p>"; } function add1() var x = document.getElementById("first").value; var y = document.getElementById("second").value; outputSum(x, y); </script> </head> js/adds2.html Demo
14
Viewing Generated Source Code
Use JavaScript to modify the DOM via an object’s innerHTML property. The browser’s View Source command shows the original unmodified HTML. Use the browser’s Inspect Element command instead to see the modified HTML. Demo
15
Checkbox Values <body> <form action=""> js/checkbox.html
<fieldset> <legend>Select one or more colors</legend> <p> <input type="checkbox" id="cbred" value="Red" /> <label for="cbred">Red</label> <input type="checkbox" id="cbgreen" value="Green" /> <label for="cbgreen">Green</label> <input type="checkbox" id="cbblue" value="Blue" /> <label for="cbblue">Blue</label> <input type="button" value="Show colors" onclick="showColors()" /> </p> </fieldset> </form> <div id="outputDiv"> <p>Output will appear here.</p> </div> js/checkbox.html
16
Checkbox Values, cont’d
<head> ... <script type="text/javascript"> function showColors() { var cbred = document.getElementById("cbred"); var cbgreen = document.getElementById("cbgreen"); var cbblue = document.getElementById("cbblue"); var output = "<p><strong>You chose:"; if (cbred.checked) output += " Red"; if (cbgreen.checked) output += " Green"; if (cbblue.checked) output += " Blue"; output += "</strong></p>"; document.getElementById("outputDiv").innerHTML = output; } </script> </head> js/checkbox.html Demo
17
Radio Button Values js/radio.html <body> <form action="">
<fieldset> <legend>Select a color</legend> <p> <input type="radio" name="colors" id="cbred" value="Red" /> <label for="cbred">Red</label> <input type="radio" name="colors" id="cbgreen" value="Green" /> <label for="cbgreen">Green</label> <input type="radio" name="colors" id="cbblue" value="Blue" /> <label for="cbblue">Blue</label> <input type="button" value="Show color" onclick="showColor()" /> </p> </fieldset> </form> <div id="outputDiv"> <p>Output will appear here.</p> </div> </body> js/radio.html
18
Radio Button Values, cont’d
<head> ... <script type="text/javascript"> function showColor() { var colors = document.getElementsByName("colors"); var output = "<p><strong>You chose:"; for (i = 0; i < colors.length; i++) { if (colors[i].checked) { output += " " + colors[i].value; } output += "</strong></p>"; document.getElementById("outputDiv").innerHTML = output; </script> </head> js/radio.html Demo
19
Select Menu Value <body> <form action=""> <fieldset>
js/backgroundcolor3.html <body> <form action=""> <fieldset> <legend>Select a color</legend> <p> <select id="colorChooser"> <option value="red">Red</option> <option value="green">Green</option> <option value="blue">Blue</option> </select> <input type="button" value="Change color" onclick="changeBGColor()" /> </p> </fieldset> </form> </body>
20
Select Menu Value, cont’d
js/backgroundcolor3.html <head> ... <script type="text/javascript"> function changeBGColor() { var color = document.getElementById("colorChooser").value; document.body.style.backgroundColor = color; } </script> </head> Demo
21
Multiple Selection Values
<body> <form action=""> <fieldset> <legend>Select one or more colors</legend> <p> <select id="colorChooser" size = "3" multiple="multiple"> <option value="red">Red</option> <option value="green">Green</option> <option value="blue">Blue</option> </select> <input type="button" value="Show colors" onclick="showColors()" /> </p> </fieldset> </form> <div id="outputDiv"> <p>Output will appear here.</p> </div> </body> js/multisel.html
22
Multiple Selection Values, cont’d
<head> ... <script type="text/javascript"> function showColors() { var chooser = document.getElementById("colorChooser"); var output = "<p><strong>You chose:"; for (i = 0; i < chooser.length; i++) { if (chooser[i].selected) { output += " " + chooser[i].value; } output += "</strong></p>"; document.getElementById("outputDiv").innerHTML = output; </script> </head> js/multisel.html Demo
23
Input Validation A key use of JavaScript code is input validation.
On the client side, validate user input in form fields before the values are submitted to the server side. The result is much more more responsive and interactive web pages.
24
Input Validation Inefficient: Too much network traffic!
The user enters form data and presses Submit. Code validates user input on the server side and discovers errors. The server code generates a new HTML page containing error messages to be displayed on the client side. Too much network traffic! The user experiences a delay between submitting the form and error messages. Poor user experience (UX).
25
Validate Non-Empty Text
HTML: JavaScript: <label>Name</label> <input type = "text" value = "" id = "name" /> name = document.getElementById("name").value; if (name == "") { errors += "Missing name.\n"; }
26
JavaScript Regular Expressions
27
Validate Phone Number HTML: JavaScript:
<label>Phone number</label> <input type = "text" value = "(nnn) nnn-nnnn" id = "phone" /> validatation/validate1.html phone = document.getElementById("phone").value; phoneRE = /^\(\d{3}\) *\d{3}-\d{4}$/; if (!phone.match(phoneRE)){ errors += "Invalid phone number. " + "Example: (999) \n"; } JavaScript quotes regular expressions with the forward /. validatation/validate1.html
28
Validate Phone Number, cont’d
left paren right paren hyphen /^\(\d{3}\) *\d{3}-\d{4}$/ any number of blanks (including none) start of string three digits three digits four digits end of string What is the purpose of specifying both the start of the string (^) and the end of the string ($)? The entire string must match (nothing before or after).
29
Validate Email Address
HTML: JavaScript: <label> address</label> <input type = "text" value = id = " " /> validatation/validate1.html = document.getElementById(" ").value; RE = if (! .match( RE)){ errors += "Invalid address. " + "Should be } validatation/validate1.html
30
Validate Email Address, cont’d
start of string end of string @ sign dot at least one character at least one character 2, 3, or 4 characters Demo
31
JavaScript Regular Expressions, cont’d
Use parentheses in a regular expression to group and store a pattern. Example: /(Bora){2}/ matches BoraBora Use \1, \2, etc. to recall the first, second, etc. match of a stored pattern. Example: /^(.)(.).*\1\2$/ matches any string that begins and ends with the same two characters, such as BoraBoraBo What can /^<(.+)>.*<\/\1>$/ match?
32
Input Validation with HTML5 and CSS3
CSS pseudo-classes: :required :invalid validatation/validate2.css input:required { border: 1px solid blue; } input:invalid { color: white; background-color: red;
33
Input Validation with HTML5 and CSS3, cont’d
<label>Name:</label> <input type = "text" value = "" id = "name" required /> <label>Phone number:</label> placeholder = "(999) " pattern = "^\(\d{3}\) *\d{3}-\d{4}$" id = "phone" <label> address:</label> placeholder = pattern = id = " " /> validation/validate2.html No JavaScript required! HTML quotes regular expressions like any other string. Demo
34
New HTML5 Input Types HTML5 has new input types.
Automatic built-in input validation. Special effects with some browsers. date number time range datetime search datetime-local week tel month url color NOTE: Different browsers implement these input types differently (Chrome does best).
35
New HTML5 Input Types, cont’d
Chrome browser: <p> <label for = "range">Range:</label> <input type = "range" id = "range" min = "0" max = "256" value = "128" /> </p> validatation/validate3.html
36
New HTML5 Input Types, cont’d
Chrome browser: <p> <label for="color">Color:</label> <input type="color" id = "color" /> </p> validate/validate3.html Demo
37
JavaScript Event Handlers
38
Assignment #2 Prototype a use case of your web application.
Client-side and server-side code. Client side: HTML, CSS, and JavaScript Text, images, and at least one form. Layout and format with CSS. User input validation with JavaScript. Server side: node.js and Express Process form data and dynamically generate pages. Due Friday, September 15.
39
JavaScript Bingo Program
Demonstrate the coordination of HTML, CSS, and JavaScript. Adapted from JavaScript, 9th edition by Tom Negrino and Dori Smith Peachpit Press, ISBN How would you implement this Bingo card? Demo
40
bingo.html The card is an HTML table.
Each cell has an id and is initially blank. <table> <tr> <th>B</th> <th>I</th> <th>N</th> <th>G</th> <th>O</th> </tr> <td id="square0"> </td> <td id="square5"> </td> <td id="square10"> </td> <td id="square14"> </td> <td id="square19"> </td> <td id="square1"> </td> <td id="square6"> </td> <td id="square11"> </td> <td id="square15"> </td> <td id="square20"> </td> <tr> <td id="square2"> </td> <td id="square7"> </td> <td id="free">Free</td> <td id="square16"> </td> <td id="square21"> </td> </tr> <td id="square3"> </td> <td id="square8"> </td> <td id="square12"> </td> <td id="square17"> </td> <td id="square22"> </td> <td id="square4"> </td> <td id="square9"> </td> <td id="square13"> </td> <td id="square18"> </td> <td id="square23"> </td> </table>
41
bingo.js window.onload = initAll; function initAll() {
document.getElementById("reload").onclick = anotherCard; newCard(); } function newCard() for (var i = 0; i < 24; i++) { setSquare(i);
42
bingo.js, cont’d Note that on a Bingo card, the numbers are distributed randomly as follows: Column B: 1 – 15 Column I: 16 – 30 Column N: 31 – 45 Column G: 46 – 60 Column O: 61 – 75 There are no repeated numbers on a card.
43
bingo.js, cont’d var colPlace = new Array(0, 0, 0, 0, 0, // B
var usedNums = new Array(76); function setSquare(thisSquare) { var currSquare = "square" + thisSquare; var colBasis = colPlace[thisSquare]*15; var newNum; do { newNum = colBasis + getNewNum() + 1; } while (usedNums[newNum]); usedNums[newNum] = true; document.getElementById(currSquare).innerHTML = newNum; document.getElementById(currSquare).className = ""; document.getElementById(currSquare).onmousedown = toggleColor; }
44
bingo.js, cont’d function getNewNum() {
return Math.floor(Math.random()*15); } function anotherCard() for (var i = 1; i < usedNums.length; i++) { usedNums[i] = false; newCard(); return false;
45
bingo.js function toggleColor(evt) { if (evt) {
var thisSquare = evt.target; } else { var thisSquare = window.event.srcElement; if (thisSquare.className == "") { thisSquare.className = "pickedBG"; thisSquare.className = ""; Dynamically change the cell’s class.
46
bingo.css body { background-color: white; color: black;
font-size: 20px; font-family: "Lucida Grande", Verdana, Arial, Helvetica, sans-serif; } h1, th { font-family: Georgia, "Times New Roman", Times, serif; h1 { font-size: 28px;
47
bingo.css, cont’d table { border-collapse: collapse; } th, td {
padding: 10px; border: 2px black solid; text-align: center; width: 20%; #free, .pickedBG { background-color: LightCoral;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.