CMPE 280 Web UI Design and Development September 7 Class Meeting

Slides:



Advertisements
Similar presentations
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
Advertisements

/k/k 1212 Cascading Style Sheets Part one Marko Boon
Tutorial 6 Creating a Web Form
CSS BASICS. CSS Rules Components of a CSS Rule  Selector: Part of the rule that targets an element to be styled  Declaration: Two or more parts: a.
Cascade Style Sheet Demo. Cascading Style Sheets Cascading Style Sheets (CSS) is a simple mechanism for adding style (e.g., fonts, colors, spacing) to.
กระบวนวิชา CSS. What is CSS? CSS stands for Cascading Style Sheets Styles define how to display HTML elements Styles were added to HTML 4.0 to.
Unit 20 - Client Side Customisation of Web Pages
Ch3: Introduction to HTML5 part 2 Dr. Abdullah Almutairi ISC 340 Fall 2014.
Chapter 6 Basic forms 1. Forms and query string 2 form : a group of user input (UI) controls that accepts information from the user and sends the information.
CS 174: Web Programming February 26 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
HTML Tables and Forms Creating Web Pages with HTML CIS 133 Web Programming Concepts 1.
Web Development & Design Foundations with XHTML Chapter 9 Key Concepts.
CST JavaScript Validating Form Data with JavaScript.
Tutorial #9 – Creating Forms. Tutorial #8 Review – Tables Borders (table, gridlines), Border-collapse: collapse; empty-cells: show; and rowspan, colspan.
Building the User Interface by Using HTML5: Organization, Input, and Validation Lesson 3.
Database-Driven Web Sites, Second Edition1 Chapter 8 Processing ASP.NET Web Forms and Working With Server Controls.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Neal Stublen Improvements  New form elements  Assist with validation  Consistency across sites  Changes reduce the need for common.
SEG3210 DHTML Tutorial. DHTML DHTML is a combination of technologies used to create dynamic and interactive Web sites. –HTML - For creating text and image.
Microsoft Visual Basic 2005 CHAPTER 7 Creating Web Applications.
ALBERT WAVERING BOBBY SENG. Week 2: HTML + CSS  Quiz  Announcements/questions/etc  Some functional HTML elements.
Introduction to JavaScript 41 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 17.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
CS 174: Web Programming September 30 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Chapter 8 Collecting Data with Forms. Chapter 8 Lessons Introduction 1.Plan and create a form 2.Edit and format a form 3.Work with form objects 4.Test.
HTML Forms. Slide 2 Forms (Introduction) The purpose of input forms Organizing forms with a and Using different element types to get user input A brief.
CS 174: Web Programming September 28 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
HTML Forms a form is a container for input elements on a web page input elements contain data that is sent to the web server for processing.
CS 174: Web Programming October 12 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
LING 408/508: Programming for Linguists
HTML Forms.
Building a Website: Layout Fall Overall Structure: Home Page Title Section Title Frame Picture UNCP Math Menu Content Footer Contact Information.
Tutorial 6 Creating a Web Form
HTML Structure II (Form) WEEK 2.2. Contents Table Form.
2440: 141 Web Site Administration Web Forms Instructor: Joseph Nattey.
>> Form Data Validation in JavaScript
Introduction to.
CGS 3066: Web Programming and Design Spring 2017
JavaScript, Sixth Edition
Chapter 5 Validating Form Data with JavaScript
Applied Component I Unit II Introduction of java-script
Chapter 6 JavaScript: Introduction to Scripting
“Under the hood”: Angry Birds Maze
Tutorial 10 Programming with JavaScript
© 2017 Akhilesh Bajaj, All Rights Reserved
JavaScript is a programming language designed for Web pages.
In this session, you will learn about:
CMPE 280 Web UI Design and Development September 21 Class Meeting
Section 17.1 Section 17.2 Add an audio file using HTML
Basic XHTML Tables XHTML tables—a frequently used feature that organizes data into rows and columns. Tables are defined with the table element. Table.
Web Development & Design Foundations with HTML5 7th Edition
IS333: MULTI-TIER APPLICATION DEVELOPMENT
Objectives Explore web forms Work with form servers
Unit 27 - Web Server Scripting
WEB PROGRAMMING JavaScript.
Integrating JavaScript and HTML
The University of Tulsa
Introduction to JavaScript
Static and Dynamic Web Pages
JavaScript Basics Topics Overview Syntactic Characteristics
Basics of Web Design Chapter 10 Form Basics Key Concepts
JavaScript Basics Topics Review Important Methods Writing Functions
JavaScript Basics What is JavaScript?
JavaScript is a scripting language designed for Web pages by Netscape.
HTML5 - 2 Forms, Frames, Graphics.
Form Validation (with jQuery, HTML5, and CSS)
Programming with Microsoft Visual Basic 2008 Fourth Edition
Introduction to JavaScript
JavaScript: BOM and DOM CS Programming Languages for Web Applications
CMPE 280 Web UI Design and Development September 10 Class Meeting
Presentation transcript:

CMPE 280 Web UI Design and Development September 7 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor: Ron Mak www.cs.sjsu.edu/~mak

The Document Object Model (DOM) Your web browser represents the contents of an HTML page internally using the Document Object Model (DOM).

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

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

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

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.

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

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

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;

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

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

Modifying the DOM <body> <form action=""> <fieldset> ... </fieldset> </form> <div id="outputDiv"> <p>Output will appear here.</p> </div> </body> js/adds2.html

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

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

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

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

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

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

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>

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

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

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

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.

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).

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"; }

JavaScript Regular Expressions

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) 999-9999\n"; } JavaScript quotes regular expressions with the forward /. validatation/validate1.html

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).

Validate Email Address HTML: JavaScript: <label>Email address</label> <input type = "text" value = "xxxxx@xxxxx.xxx" id = "email" /> validatation/validate1.html email = document.getElementById("email").value; emailRE = /^.+@.+\..{2,4}$/; if (!email.match(emailRE)){ errors += "Invalid email address. " + "Should be xxxxx@xxxxx.xxx\n"; } validatation/validate1.html

Validate Email Address, cont’d start of string end of string @ sign dot /^.+@.+\..{2,4}$/ at least one character at least one character 2, 3, or 4 characters Demo

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?

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;

Input Validation with HTML5 and CSS3, cont’d <label>Name:</label> <input type = "text" value = "" id = "name" required /> <label>Phone number:</label> placeholder = "(999) 999-999" pattern = "^\(\d{3}\) *\d{3}-\d{4}$" id = "phone" <label>Email address:</label> placeholder = "xxxxx@xxxxx.xxx" pattern = "^.+@.+\..{2,4}$" id = "email" /> validation/validate2.html No JavaScript required! HTML quotes regular expressions like any other string. Demo

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 email week tel month url color NOTE: Different browsers implement these input types differently (Chrome does best).

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

New HTML5 Input Types, cont’d Chrome browser: <p> <label for="color">Color:</label> <input type="color" id = "color" /> </p> validate/validate3.html Demo

JavaScript Event Handlers

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.

JavaScript Bingo Program Demonstrate the coordination of HTML, CSS, and JavaScript. Adapted from JavaScript, 9th edition by Tom Negrino and Dori Smith Peachpit Press, 2015 ISBN 978-0-321-99670-1 How would you implement this Bingo card? Demo

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>

bingo.js window.onload = initAll; function initAll() { document.getElementById("reload").onclick = anotherCard; newCard(); } function newCard() for (var i = 0; i < 24; i++) { setSquare(i);

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.

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; }

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;

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.

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;

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;