JavaScript Basics Topics Review Important Methods Writing Functions

Slides:



Advertisements
Similar presentations
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
Advertisements

The Web Warrior Guide to Web Design Technologies
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 7 Event-Driven.
CIS101 Introduction to Computing Week 10 Spring 2004.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
CSC318 – DYNAMIC WEB APPLICATION DEVELOPMENT
JavaScript Part 1.
1 CLIENT-SIDE SCRIPTS. Objectives 2 Learn how to reference objects in HTML documents using the HTML DOM and dot syntax Learn how to create client-side.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
DHTML: Dynamic HTML Internet Technology1. What is DHTML? A collection of enhancements to HTML ► To create dynamic and interactive websites Combination.
CC1003N Week 6 More CSS and Javascript.. Objectives: ● More CSS Examples ● Javascript – introduction ● Uses of Javascript ● Variables ● Comments ● Control.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
1 JavaScript Functions and Objects. 2 Objectives You will be able to Use JavaScript functions in JavaScript scripts. Get JavaScript functions executed.
05 – Java Script (1) Informatics Department Parahyangan Catholic University.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
Functions.  Assignment #2 is now due on Wednesday, Nov 25 th  (No Quiz)  Go over the midterm  Backtrack and re-cover the question about tracing the.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
Javascript Overview. What is Javascript? May be one of the most popular programming languages ever Runs in the browser, not on the server All modern browsers.
Event Handling (the right way). A Simple Web Page Events - Summary The web page looks like this:
Document Object Model Nasrullah. DOM When a page is loaded,browser creates a Document Object Model of the Page.
CSC 121 Computers and Scientific Thinking Fall Event-Driven Programming.
Client-side (JavaScript) Validation. Associating a function with a click event – Part 1 Use the input tag’s onclick attribute to associate a function.
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
A little more JavaScript??. Building a caculator We'll need to learn about the following: – Form buttons. – Applying CSS rules by attribute values. –
REEM ALMOTIRI Information Technology Department Majmaah University.
XHTML Forms.
Introduction to.
Build in Objects In JavaScript, almost "everything" is an object.
Event-Driven Programming
The Box Model in CSS Web Design – Sec 4-8
Applied Component I Unit II Introduction of java-script
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Internet of the Past.
>> JavaScript: Location, Functions and Objects
The Box Model in CSS Web Design – Sec 4-8
© 2017 Akhilesh Bajaj, All Rights Reserved
© 2015, Mike Murach & Associates, Inc.
JavaScript functions.
Project 9 Creating Pop-up Windows, Adding Scrolling Messages, and Validating Forms.
Barb Ericson Georgia Institute of Technology May 2006
JavaScript Functions.
The Box Model in CSS Web Design – Sec 4-8
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
14 A Brief Look at JavaScript and jQuery.
Revision.
JavaScript Introduction
JavaScript Basics Topics Overview Syntactic Characteristics
DHTML Javascript Internet Technology.
Understanding JavaScript and Coding Essentials
Event Driven Programming & User Defined Functions
DHTML Javascript Internet Technology.
The University of Tulsa
Chapter 6 Event-Driven Pages
JavaScript Basics Topics Overview Syntactic Characteristics
CSC318 – DYNAMIC WEB APPLICATION DEVELOPMENT
How JavaScript and jQuery are used to enhance web pages
HTML5 and Local Storage.
Java Script Siddharth Srivastava.
Chapter 7 Event-Driven Pages
Introduction to Programming and JavaScript
JavaScript: Introduction to Scripting
CNIT 133 Interactive Web Pags – JavaScript and AJAX
Chapter 4 - JavaScript Events, Objects, & Functions
Web Programming and Design
JavaScript: BOM and DOM CS Programming Languages for Web Applications
Murach's JavaScript and jQuery (3rd Ed.)
Presentation transcript:

JavaScript Basics Topics Review Important Methods Writing Functions JavaScript Event Handlers Practice: Carving out divisions for JavaScript Object Models Creating an App

Review JavaScript document document is the object that lets you work with the Document Object Model (DOM) that represents HTML elements of a page. Example: document.open() will open/load a document for writing. document.write() will write to a document that has been loaded.

Review $ $ is a valid JavaScript identifier. The $ was intended to be used for machine-generated variables (such as $0001). The $ represents a jQuery Function, and is actually a shorthand alias for jQuery. $ symbol is not reserved, and may be used as a variable name.) $ is typically used as a selector (i.e. a function that returns a set of elements found in the DOM).

Review: Building content with JavaScript <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Practice JavaScript</title> <script src="js/navigation.js"></script> </head> <body> </body> </html> HTML The JavaScript file is located in the js folder.

Methods One of the most important methods using in JavaScript is is getElementById(). A second important method is parseInt() and parseFloat(). These are available through the window object.

Accessing Document Objects using JavaScript getElementById() is used to retrieve the object that represents an HTML element. getElementById() requires the id for the element as the parameter. Example: var name = document.getElementById(“name”);

Parsing input from the User in JavaScript parseInt() and parseFloat() are used to convert a string to an integer or a float. If a string cannot be converted to a numeric value, these methods will return NaN (Not a Number)

JavaScript Functions JavaScript provides for two kinds of functions: Function expression: A variable is assigned the value returned by a function expression. These functions are not given a name, they are often referred to as anonymous functions. Function declaration: Function declarations are equivalent to methods in Java.

Function Expression Example 1 The following function expression returns a DOM element: var $ = function (id) { return document.getElementById(id); } A statement that calls the $ function might be: var address = $(“email_address”).value; NOTE: $ has no specific meaning and is used to access anonymous functionality.     

Function Expression Example 2 The following function expression returns a DOM element: var displayYear = function () { var today = new Date(); alert (“The year is “+today.getFullYear()); } A statement that calls the $ function might be: displayYear();

Function Definition Example 1 The following function returns the smallest integer value : function smallest (x, y) { return (x < y) ? x: y; } A statement that calls the function might be: var min = smallest(x, y);

JavaScript EventHandlers An event handler is a function that will execute when an event is triggered. Technically, an event handler “handles” an event. To attach an event handler to an event, specify the object and the event that triggers the event handler. Finally, assign the event handler function to the specific event.

Consider the following HTML button: Event Handler Example Consider the following HTML button: <input type="button" class="btn" id="computeBtn" value="Compute It"> An event listener can be assigned as follows: window.onload = function() { var computeBtn = document.getElementById("computeBtn"); computeBtn.onclick = computeValue(); }

Practice: Miles Per Gallon Web App Examine the mockup and carve out divisions. Focus will be on JavaScript, not CSS.

Practice: Miles Per Gallon Web App HTML <body> <div> <div class=”header"> <h1>Calculate MPG</h1> </div>   <div class="content"> <label for="milesdriven" class="mlabel">Miles Driven:</label> <input type="text" id="milesdriven" class="mfield"><br> <label for="ngallons">Gallons of Gas Used:</label> <input type="text" id="ngallons"><br> <label for="milespergallon">Miles Per Gallon:</label> <input type="text" id="milespergallon" disabled><br> <label> </label> <input type="button" class="btn" id="calculate" value="Calculate MPG"><br> <div class=”footer"> <p><i>Solutions by Bobo<i></p> </body>

Practice: Miles Per Gallon Web App CSS h1 { padding: 0 2.75em .5em; } .content { width: 100%; padding-top: 20px; border: 1px solid gray; label { float: left; width: 11em; text-align: right; input { margin-left: 1em; margin-bottom: .5em;

Solution: Miles Per Gallon Web App JavaScript window.onload = function() { // REGISTER AN ON CLICK EVENT FOR THE CALULATE BUTTON var calculateBtn = document.getElementById("calculate"); calculateBtn.onclick = processMPG; // PLACE THE FOCUS ON THE MILES DRIVEN FIELD var milesDrivenField = document.getElementById("milesdriven"); milesDrivenField.focus(); } var processMPG = function() { // TASK 1: COLLECT THE INPUT FOR MILES DRIVEN // TASK 2: COLLECT THE INPUT FOR GALLONS USED // TASK 3: VALIDATE THE INPUT // TASK 4: COMPUTE AND DISPLAY MILES PER GALLON

Modify the JavaScript * Include an alias function for returning an element Id var $ = function (id) { return document.getElementById(id); }

Event Handler Options Option 1: In a JavaScript file, attach an onclick() listener to the DOM element: window.onload = function() { var myBtn = document.getElementById(buttonIdName); myBtn.onclick = eventHandlerName; } Option 2: Attach a listener directly in HTML <button onclick="eventHandlerName();"> Compute ; </button>