CS 174: Web Programming October 12 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Slides:



Advertisements
Similar presentations
JavaScript and AJAX Jonathan Foss University of Warwick
Advertisements

JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
HTML 5 and CSS 3, Illustrated Complete Unit L: Programming Web Pages with JavaScript.
JavaScript Forms Form Validation Cookies CGI Programs.
CS 299 – Web Programming and Design Overview of JavaScript and DOM Instructor: Dr. Fang (Daisy) Tang.
CS 160: Software Engineering August 27 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 174: Web Programming February 26 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
JQuery. What is jQuery? jQuery is a fast, small, and feature-rich JavaScript library that simplifies HTML document traversing and manipulation event handling.
CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
JavaScript Demo Presented by … Jaisingh Sumit jain Sudhindra Taran Deep arora.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
Forms, Validation Week 7 INFM 603. Announcements Try placing today’s example in htdocs (XAMPP). This will allow you to execute examples that rely on PHP.
CS 46B: Introduction to Data Structures July 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Christopher Paolini Computational Science Research Center College of Engineering San Diego State University Computational Science 670 Fall 2009 Monday.
4.1 JavaScript Introduction
Database-Driven Web Sites, Second Edition1 Chapter 8 Processing ASP.NET Web Forms and Working With Server Controls.
Review and Practice for Final exam. Final exam Date: December 20, 3:15 – 5:15pm Format: Two parts: First part: multiple-choice questions (15 questions.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
DOM and JavaScript Aryo Pinandito.
JavaScript is a client-side scripting language. Programs run in the web browser on the client's computer. (PHP, in contrast, is a server-side scripting.
Integrating JavaScript and HTML5 HTML5 & CSS 7 th Edition.
CS 235: User Interface Design October 15 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 174: Web Programming September 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
5.2 DOM (Document Object Model). 2 Motto: To write it, it took three months; to conceive it three minutes; to collect the data in it — all my life. —F.
Tutorial 10 Programming with JavaScript
Done by: Hanadi Muhsen1 Tutorial 1.  Learn the history of JavaScript  Create a script element  Write text to a Web page with JavaScript  Understand.
School of Computing and Information Systems CS 371 Web Application Programming PHP – Forms, Cookies, Sessions and Database.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Introduction to JavaScript 41 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 17.
CS 174: Web Programming September 30 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 235: User Interface Design September 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 160: Software Engineering October 6 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
CS 174: Web Programming August 31 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 174: Web Programming September 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. HTML5 Evolving Standards JavaScript.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
CS 174: Web Programming September 28 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
1 Javascript DOM Peter Atkinson. 2 Objectives Understand the nature and structure of the DOM Add and remove content from the page Access and change element.
Set 2: DOM IT452 Advanced Web and Internet Systems.
Creating Dynamic Webpages
CS 174: Web Programming October 14 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
LING 408/508: Programming for Linguists Lecture 11 October 5 th.
Advanced DOM Builds on last presentation on DOM Allows you to dynamically create elements and position them on a page DOM methods/properties are W3C standard.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
HTML DOM Basharat Mahmood, Department of Computer Science, CIIT, Islamabad, Pakistan 1.
CS 174: Web Programming November 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
CS 160 and CMPE/SE 131 Software Engineering February 11 Class Meeting Department of Computer Science Department of Computer Engineering San José State.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
JavaScript and AJAX 2nd Edition Tutorial 1 Programming with JavaScript.
CS 174: Web Programming October 28 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Javascript Prof. Wenwen Li School of Geographical Sciences and Urban Planning 5644 Coor Hall
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
JavaScript, Sixth Edition
Javascript and Dynamic Web Pages: Client Side Processing
CS 330 Class 7 Comments on Exam Programming plan for today:
In this session, you will learn about:
CMPE 280 Web UI Design and Development September 21 Class Meeting
* Lecture # 7 Instructor: Rida Noor Department of Computer Science
PHP Training at GoLogica in Bangalore
CMPE 280 Web UI Design and Development February 22 Class Meeting
Murach's JavaScript and jQuery (3rd Ed.)
CMPE 280 Web UI Design and Development February 21 Class Meeting
Presentation transcript:

CS 174: Web Programming October 12 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak More JavaScript Regular Expressions  JavaScript uses regular expressions for more than just pattern matching.  You can use regular expressions also for string manipulation. 2

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak Example: Reversing Names  Suppose you had a list of names, such as  You want to reverse the names, last name first, and insert a comma: 3 Gerald Ford Ronald Reagan George Bush Bill Clinton Ford, Gerald Reagan, Ronald Bush, George Clinton, Bill Demo

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak Reversing Names, cont’d 4 Reverse names: Enter a list of names with first name first, one per line: <input type="button" value="Reverse names" onclick=reverseNames() /> re/reverse.html

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak Reversing Names, cont’d 5 function reverseNames() { var names = document.getElementById("names").value; var splitter = /\s*\n\s*/; var nameList = names.split(splitter); var newList = new Array; var reverser = /(\S+)\s(\S+)/; for (var i = 0; i < nameList.length; i++) { if (nameList[i].trim().length > 0) { newList[i] = nameList[i].replace(reverser, "$2, $1"); newNames += newList[i] + "\n"; } } document.getElementById("names").value = newNames; } Use the matching pattern to split the string into an array of names \s = whitespace character Replace each name string according to the matching pattern. We can improve this regular expression! \S = non-whitespace character re/reverse.js

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak Example: Formatting Names  Suppose you allow users to be sloppy:  But you still want: 6 gerald ford RONALD REAGAN GeOrGe BuSh BiLL CLinTON Ford, Gerald Reagan, Ronald Bush, George Clinton, Bill Demo

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak Formatting Names, cont’d  Our regular expression for formatting names: Split the first and last names each into an initial letter followed by the rest of the letters.  Called the regular expression’s exec() method on a string. Automatically sets JavaScript’s built-in RegExp object. Use RegExp.$1, RegExp.$2, etc. to access stored parts of the match. 7 var formatter = /\s*(\S)(\S+)\s+(\S)(\S+)\s*/

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak Formatting Names, cont’d 8 var newNames = ""; for (var i = 0; i < nameList.length; i++) { if (nameList[i].trim().length > 0) { formatter.exec(nameList[i]); newList[i] = RegExp.$3.toUpperCase() + RegExp.$4.toLowerCase() + ", " + RegExp.$1.toUpperCase() + RegExp.$2.toLowerCase(); newNames += newList[i] + "\n"; } } re/format.js

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak More Document Object Model (DOM)  Recall the DOM. Example: 9 JavaScript, 9 th ed. by Tom Negrino and Dori Smith Peachpit Press, 2015 ISBN

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak DOM, cont’d  Also recall how we used innerHTML and JavaScript to modify the DOM: Output will appear here. document.getElementById("outputDiv").innerHTML = " " + x + " + " + y + " = " + sum + " ”;

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak DOM, cont’d  Using innerHTML is error-prone. You can create elements with unclosed tags, or invalid tags.  A safer way to modify the DOM is to use JavaScript’s DOM manipulation API. 11 document.getElementById("outputDiv").innerHTML = " " + x + " + " + y + " = " + sum + " "; Demo

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak Example: DOM Modification  The root node whose children we will manipulate using JavaScript’s DOM API: 12 dom/nodes.html

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak DOM Modification, cont’d 13 window.onload = init; var textArea; var chooser; var indexer; var rootNode; function init() { textArea = document.getElementById("textArea"); chooser = document.getElementById("chooser"); indexer = document.getElementById("indexer"); root = document.getElementById("root"); } dom/nodes.js

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak DOM Modification: Add a Child Node 14 function addNode() { var text = textArea.value; var textNode = document.createTextNode(text); var pNode = document.createElement("p"); pNode.appendChild(textNode); rootNode.appendChild(pNode); textArea.value = ""; } dom/nodes.js

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak DOM Modification: Insert a Child Node 15 function insertNode() { var textNode = textArea.value; var index = indexer.selectedIndex; var textNode = document.createTextNode(textNode); var pNode = document.createElement("p"); pNode.appendChild(textNode); var pNodes = rootNode.getElementsByTagName("p"); var chosenPNode = pNodes.item(index); rootNode.insertBefore(pNode, chosenPNode); textArea.value = ""; } dom/nodes.js

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak DOM Modification: Replace a Child Node 16 function replaceNode() { var text = textArea.value; var index = indexer.selectedIndex; var textNode = document.createTextNode(text); var pNode = document.createElement("p"); pNode.appendChild(textNode); var pNodes = rootNode.getElementsByTagName("p"); var chosenPNode = pNodes.item(index); rootNode.replaceChild(pNode, chosenPNode); textArea.value = ""; } dom/nodes.js

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak DOM Modification: Delete a Child Node 17 function deleteNode() { var index = indexer.selectedIndex; var pNodes = rootNode.getElementsByTagName("p"); var chosenPNode = pNodes.item(index); rootNode.removeChild(chosenPNode); } dom/nodes.js

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak Custom JavaScript Objects  At run time, a JavaScript variable can have any value.  Create a custom object simply by giving it properties and methods. 18

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak Example Custom JavaScript Object 19 var person = new Object(); person.name = "Mary"; person.age = 20; person.nextYear = function() { return this.age + 1; }; alert("Name = " + this.name + ", age = " + this.age + ", age next year = " + this.nextYear()); Demo objects/person.html

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak JavaScript Classes and Objects  A JavaScript class has a constructor function. Example: 20 function Person(name, age) { this.name = name; this.age = age; this.nextYear = function() { return this.age + 1; }; } Convention: Capitalized constructor name.

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak Example Object Instantiation  Use the constructor to create new instances: 21 function createPeople() { mary = new Person("Mary", 20); john = new Person("John", 25); showPerson(mary); showPerson(john); } function showPerson(p) { alert("Name = " + p.name + ", age = " + p.age + ", age next year = " + p.nextYear()); } Demo objects/people.html

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak Prototype Objects  A JavaScript class is a set of objects that inherit properties from the same prototype object. 22 Person.prototype = { toString: function() { return "Person[name: '" + this.name + "', age: " + this.age + "]"; } } function createPeople() { mary = new Person("Mary", 20); john = new Person("John", 25); alert(mary); alert(john); } Demo objects/prototype.html

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak Hybrid Web Pages  Now we can have pages that contain: HTML CSS  … JavaScript  … PHP  23

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak Hybrid Web Pages, cont’d  Remember that HTML, CSS, and JavaScript are interpreted by the client-side web browser. Firefox, Chrome, Apache, etc.  Remember that PHP is interpreted by the server-side web server. Apache, etc. 24

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak Hybrid Web Pages, cont’d  All web pages live in the web server’s htdocs (or public_html ) directory. Access each page via its URL.  Pages containing PHP must go through the web server for the PHP code to be executed. If you simply open such a page in a web browser, the HTML, CSS, and JavaScript code may work, but the PHP code will not execute. PHP code is executed by the web server. 25

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak Hybrid Web Pages, cont’d  A web page can contain PHP code that generates a new page containing HTML, CSS, JavaScript, and even PHP code.  TIP: To avoid confusion, have your web pages contain only HTML code as much as possible. Link to separate CSS and JavaScript files. Include separate PHP files. 26

Computer Science Dept. Fall 2015: October 12 CS 174: Web Programming © R. Mak Coming Soon …  JQuery A JavaScript framework that simplifies client-side programming.  AJAX A JavaScript technique for communicating with the web server that is more efficient than having the server download a new web page each time. 27