Web Development & Design Foundations with HTML5

Similar documents
8/19/2018. Web Development & Design Foundations with HTML5. Learning Objectives (1 of 2) Learning Objectives (2 of 2) What is JavaScript?

Web Development & Design Foundations with HTML5, 8 th Edition Instructor Materials Chapter 14 Test Bank

UI Course HTML: (Html, CSS, JavaScript, JQuery, Bootstrap, AngularJS) Introduction. The World Wide Web (WWW) and history of HTML

jquery Tutorial for Beginners: Nothing But the Goods

Introduction to JavaScript

Introduction to using HTML to design webpages

Fundamentals of Website Development

jquery Element & Attribute Selectors, Events, HTML Manipulation, & CSS Manipulation

JavaScript By: A. Mousavi & P. Broomhead SERG, School of Engineering Design, Brunel University, UK

PHP / MYSQL DURATION: 2 MONTHS

WELCOME TO JQUERY PROGRAMMING LANGUAGE ONLINE TUTORIAL

By the end of this section of the practical, the students should be able to:

New Perspectives on Creating Web Pages with HTML. Tutorial Objectives

CISH-6510 Web Application Design and Development. Overview of JavaScript. Overview

Web Programming and Design. MPT Junior Cycle Tutor: Tamara Demonstrators: Aaron, Marion, Hugh

New Media Production Lecture 7 Javascript

Tizen Web UI Technologies (Tizen Ver. 2.3)

729G26 Interaction Programming. Lecture 4

SEEM4570 System Design and Implementation Lecture 04 jquery

LECTURE-3. Exceptions JS Events. CS3101: Programming Languages: Javascript Ramana Isukapalli

HCDE 530: Computational Techniques for HCDE Data Visualization in Web, Part 2

CS7026. Introduction to jquery

CHAPTER 6 JAVASCRIPT PART 1

A Balanced Introduction to Computer Science, 3/E

EVENT-DRIVEN PROGRAMMING

JavaScript and XHTML. Prof. D. Krupesha, PESIT, Bangalore

Q1. What is JavaScript?

Client vs Server Scripting

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2017)

NAVIGATION INSTRUCTIONS

8/19/2018. Web Development & Design Foundations with HTML5. Learning Objectives (1 of 2) Learning Objectives (2 of 2) Helper Applications & Plug-Ins

showinplaceholder The jquery Plug-in

BEFORE CLASS. If you haven t already installed the Firebug extension for Firefox, download it now from

JavaScript s role on the Web

Place User-Defined Functions in the HEAD Section

UNIT 3 SECTION 1 Answer the following questions Q.1: What is an editor? editor editor Q.2: What do you understand by a web browser?

"a computer programming language commonly used to create interactive effects within web browsers." If actors and lines are the content/html......

Lecture 14. Introduction to JavaScript. Mr. Mubashir Ali Lecturer (Dept. of Computer Science)

Web Designing Course

710 Index Attributes, 127 action attribute, 263 assigning, bottom attribute, domain name attribute, 481 expiration date attribute, 480 8

The figure below shows the Dreamweaver Interface.

Lesson 5: Introduction to Events

Client Side Scripting. The Bookshop

INTRODUCTION TO JAVASCRIPT

As we design and build out our HTML pages, there are some basics that we may follow for each page, site, and application.

Chapter 9 Introducing JQuery

grabattention The jquery Plug-in

JS Tutorial 3: InnerHTML Note: this part is in last week s tutorial as well, but will be included in this week s lab

Using Dreamweaver CC. 6 Styles in Websites. Exercise 1 Linked Styles vs Embedded Styles

1. Cascading Style Sheet and JavaScript

CGS 3066: Spring 2015 JavaScript Reference

NEW WEBMASTER HTML & CSS FOR BEGINNERS COURSE SYNOPSIS

Module 5 JavaScript, AJAX, and jquery. Module 5. Module 5 Contains 2 components

Want to add cool effects like rollovers and pop-up windows?

1. Setup a root folder for the website 2. Create a wireframe 3. Add content 4. Create hyperlinks between pages and to external websites

Client-side Techniques. Client-side Techniques HTML/XHTML. Static web pages, Interactive web page: without depending on interaction with a server

Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world

Introduction to DHTML

JavaScript I.b CSCI311

The first sample. What is JavaScript?

JavaScript CS 4640 Programming Languages for Web Applications

E ECMAScript, 21 elements collection, HTML, 30 31, 31. Index 161

Netscape Introduction to the JavaScript Language

Using Dreamweaver CS6

CE212 Web Application Programming Part 2

CSC 337. jquery Rick Mercer

HTML5. HTML5 Introduction. Form Input Types. Semantic Elements. Form Attributes. Form Elements. Month Number Range Search Tel Url Time Week

Using Dreamweaver CS6

HTML 5 and CSS 3, Illustrated Complete. Unit L: Programming Web Pages with JavaScript

Manual Html A Href Onclick Submit Form

CS197WP. Intro to Web Programming. Nicolas Scarrci - February 13, 2017

Website Development with HTML5, CSS and Bootstrap

JavaScript: Introduction, Types

A340 Laboratory Session #5

Produced by. App Development & Modeling. BSc in Applied Computing. Eamonn de Leastar

Events: another simple example

Session 17. jquery. jquery Reading & References

Chapter 3 - Simple JavaScript - Programming Basics. Lesson 1 - JavaScript: What is it and what does it look like?

Guidelines for doing the short exercises

Web Site Development with HTML/JavaScrip

PIC 40A. Midterm 1 Review

The Structure of the Web. Jim and Matthew

Make a Website. A complex guide to building a website through continuing the fundamentals of HTML & CSS. Created by Michael Parekh 1

INTRODUCTION TO WEB DEVELOPMENT AND HTML. Lecture 13: Intro to JavaScript - Spring 2011

JavaScript!= Java. Intro to JavaScript. What is JavaScript? Intro to JavaScript 4/17/2013 INTRODUCTION TO WEB DEVELOPMENT AND HTML

Animating Layers with Timelines

LECTURE-2. Functions review HTML Forms. Arrays Exceptions Events. CS3101: Scripting Languages: Javascript Ramana Isukapalli

Lesson 5 Introduction to Cascading Style Sheets

Module 5 JavaScript, AJAX, and jquery. Module 5. Module 5 Contains an Individual and Group component

Introduction to. Maurizio Tesconi May 13, 2015

COURSE 20480B: PROGRAMMING IN HTML5 WITH JAVASCRIPT AND CSS3

Lecture 5. Connecting Code to Web Page Events

Programing for Digital Media EE1707. Lecture 4 JavaScript By: A. Mousavi & P. Broomhead SERG, School of Engineering Design, Brunel University, UK

of numbers, converting into strings, of objects creating, sorting, scrolling images using, sorting, elements of object

Programming in HTML5 with JavaScript and CSS3

CIS 228 (Spring, 2012) Final, 5/17/12

Standard 1 The student will author web pages using the HyperText Markup Language (HTML)

v0.9.3 Tim Neil Director, Application Platform & Tools Product

Introduction to WEB PROGRAMMING

Varargs Training & Software Development Centre Private Limited, Module: HTML5, CSS3 & JavaScript

Transcription:

1

Web Development & Design Foundations with HTML5 CHAPTER 14 A BRIEF LOOK AT JAVASCRIPT Copyright Terry Felke-Morris 2

Learning Outcomes In this chapter, you will learn how to: Describe common uses of JavaScript in web pages. Describe the purpose of the Document Object Model and list some common events. Create a simple JavaScript using the script element and the alert() method. Use variables, operators and the if control structure. Create a basic form validation script. Describe common uses of jquery. Describe how to obtain jquery. Use jquery selectors and methods. Configure an image gallery with jquery. Describe the purpose of jquery plugins. 3

What is JavaScript? Object-based scripting language Works with the objects associated with a Web page document the window the document the elements such as forms, images, hyperlinks, etc. 4

What is JavaScript? Originally developed by Netscape Named LiveScript Netscape & Sun Microsystems Collaboration LiveScript renamed JavaScript JavaScript is NOT Java 5

Common Uses of JavaScript Display a message box Select list navigation Edit and validate form information Create a new window with a specified size and screen position Image Rollovers Status Messages Display Current Date Calculations 6

Coding JavaScript JavaScript statements can be coded on a web page using two different techniques: Place JavaScript code between <script> tags Place JavaScript code as part of an event attached to an HTML element 7

JavaScript: Using the script Element The script element A container tag May be placed in either the head or the body section of a web page <script type="text/javascript"> <!- - alert("welcome to Our Site"); // - -> </script> Alert Demo 8

Document Object Model (DOM) A portion of the DOM is shown at the left. Defines every object and element on a web page Hierarchical structure Accesses page elements and apply styles to page elements 9

Object An object is a thing or entity. Browser window Submit button Web page document 10

Property A property is a characteristic or attribute of an object. The background color of a web page document document.bgcolor The date the web page file was last modified document.lastmodified The src file of an image object image1.src 11

Method A method is an action (a verb) Writing text to a web page document document.write() Submitting a form form1.submit() 12

JavaScript and Events Events: actions taken by the web page visitor clicking (onclick), placing the mouse on an element (onmouseover), removing the mouse from an element (onmouseout), loading the page (onload), unloading the page (onunload), etc. 13

Events Event click load mouseover Event Handler onclick onload onmouseover mouseout onmouseout submit unload onsubmit onunload 14

JavaScript and Events JavaScript can be configured to perform actions when events occur. The event name is coded as an attribute of an HTML tag The value of the event attribute contains the JavaScript code Example: Display an alert box when the mouse is placed over a hyperlink. <a href="home.htm" onmouseover="alert('click to go home')">home</a> 15

JavaScript Debugging(1) Check the syntax of the statements Pay very close attention to upper and lower case letters, spaces, and quotations Verify that you have saved the page with your most recent changes Verify that you are testing the most recent version of the page (refresh or reload the page) If you get an error message, use the error messages that are displayed by the browser In Firefox: Select Tools > Error Console 16

JavaScript Debugging(2) Using the Firefox browser: Select the top right menu icon ( Hamburger icon) Select Developer > Web Console The Web Console will indicate an issue and the line number This may not be exactly where the problem is Sometimes the error is one or two lines above the indicated line number. 17

Variable A variable is a placeholder for information. The variable is stored in the computer s memory (RAM). var username; username = "Karen"; document.write(username); Demo 18

Prompts prompt() method Displays a message and accepts a value from the user myname = prompt( prompt message ); The value typed by the user is stored in the variable myname Demo 19

Arithmetic Operators Operator Description Example Value of Quantity = assign quantity = 10 10 + addition quantity = 10 + 6 16 - subtraction quantity = 10-6 4 * multiplication quantity = 10 * 2 20 / division quantity = 10 / 2 5 20

Comparison Operators Operator Description Example Sample values of quantity that would result in true = = Double equals sign (equivalent) is exactly equal to quantity = = 10 10 > Greater than quantity > 10 11, 12 (but not 10) > = Greater than or equal to quantity > = 10 10, 11, 12 < Less than quantity < 10 8, 9 (but not 10) < = Less than or equal to quantity < = 10 8, 9, 10! = Not equal to quantity! = 10 8, 9, 11 (but not 10) 21

Decision Making if (condition) { commands to execute if condition is true } else { commands to execute if condition is false } 22

Function A function is a block of one or more JavaScript statements with a specific purpose, which can be run when needed. function function_name() { }... JavaScript statements Demo 23

Using Functions Defining the Function function showalert() { alert("please click OK to continue."); } Calling the Function showalert(); 24

Form Validation It is common to use JavaScript to validate form information before submitting it to the web server. Is the name entered? Is the e-mail address of correct format? Is the phone number in the correct format? See Hands-on Practice 14.8 25

Validating Form Fields Use the "" or null to check to determine if a form field has information if (document.forms[0].username.value == "" ) { alert("name field cannot be empty."); return false; } // end if Demo 26

JavaScript & Accessibility Don t expect JavaScript to always function for every visitor Some may have JavaScript disabled Some may be physically unable to click a mouse Provide a way for your site to be used if JavaScript is not functioning Plain text links E-mail contact info 27

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>put YOUR TITLE HERE</title> <link rel="stylesheet" href="css/styles.css" type="text/css"> <script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.js" type="text/javascript"></script> <script src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function() { $("h3").css("background-color", "green"); $("h3").css("color", "yellow"); }); </script> </head> <body> <h3>i have changed the font color to yellow and the background color to green for all h3 elements on this page using jquery.</h3> </body> </html> 28

<body> <script type="text/javascript"> $(document).ready(function() { window.alert("welcome, if you enjoy the site, please feel free to sign up for our monthly newsletter on the Newsletter page."); }); </script> </body> 29

<script> window.alert("sign up today and we'll send you a free copy of our Accross the Timelines Magazine Free!"); </script> 30

<head> <script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.js" type="text/javascript"></script> <script src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.js" type="text/javascript"></script> <script type="text/javascript"> <!-- the function below is called by the <body> tag causing an alert message when the page is first loaded --> function MOTD() { alert( "Ready to Have Fun with Hexadecimal??? Click OK and Get Started!!!" ) ; } </script> </head> <!-- the body tag calls the Message of the Day - MOTD function defined in the head section --> <body onload="motd()"> 31

<!-- picture swapping for the hexadecimal Welcome! message below --> <a href="" onmouseover="document.myimage.src='img/welcome2.jpg';" onmouseout="document.myimage.src='img/cores.jpg';"> <img id="myimage" src="img/cores.jpg" height="169" width="514" alt="ferrite"> </a> 32

<!-- Javascript needs to be defined AFTER its HTML references - so at the bottom of the code is best --> This page was last modified on: <script type="text/javascript"> document.write(document.lastmodified); </script> 33

What is jquery? jquery is a free open-source JavaScript Library Provides interaction and dynamic effects on web pages Resources http://jquery.com http://jquery.org 34

Common Uses of JQuery Dynamically manipulate the CSS properties of elements Detect and react to events such as mouse movements Animate elements on a web page such as image slideshows And much more... 35

Adding jquery to a Web Page Two Options: Download jquery http://jquery.com/download Access jquery via a CDN <script src= "http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"> </script> 36

The Ready Event Triggered when the browser has loaded the Document Object Model(DOM) for the web page $(document).ready(function() { Your JavaScript statements and other jquery statements go here }) 37

Display an Alert When the page loads <script> $(document).ready(function() { alert("ready for jquery"); }) </script> 38

jquery Selectors A selector indicates which DOM elements jquery will affect http://api.jquery.com/category/selectors Some commonly used jquery selectors Selector $('*') $('li') $('.myclass') $('#myid') $('nav a') $('#resources a') $('li:first') $('li:odd') Purpose wildcard selects all elements HTML element selector selects all li elements Class selector selects all elements assigned to the class named myclass Id selector selects the element assigned to the id named myid HTML element selector selects all anchor elements contained within the nav element Id selector and HTML element selector selects all anchor elements contained within the id named resources Positional selector that selects the first element of that type on the page Positional selector- selects every other li element on the page 39

jquery Methods A method acts upon the DOM elements you have selected http://api.jquery.com Some commonly used jquery methods Method click() css() fadetoggle() hover() slidetoggle() toggle() attr() html() Purpose Binds a jquery event handler to the JavaScript click event Sets the specified CSS property for the selected element(s Displays or hides the selected element(s) by animating their opacity Binds a jquery event handler to the JavaScript onmouseover event Displays or hides the selected element(s) with a sliding motion Displays or hides the selected element(s) Gets or sets attributes for the selected element(s) Gets or sets HTML contents for the selected element(s) 40

Using the click() and CSS() Methods <script> $(document).ready(function() { $('a').click(function(){ }); </script> $('li:even').css('color','#006600'); }); 41

Using the toggle() Method <script> $(document).ready(function() { $('#more').click(function(){ $('#details').toggle(); }); }); </script> 42

jquery Image Gallery <script> $(document).ready(function(){ $('#gallery a').click(function(){ var galleryhref = $(this).attr('href'); var galleryalt = $(this).attr('title'); $('figure img').attr({ src: galleryhref, alt: galleryalt }); $('figcaption').html(galleryalt); return false; }); }); </script> 43

jquery Plugin JavaScript that extends the functionality of jquery http://plugins.jquery.com MIT license: http://opensource.org/licenses/mit Examples: fotorama plugin http://plugins.jquery.com/fotorama Validate plugin http://plugins.jquery.com/validation 44