JavaScript Basics Topics Overview Syntactic Characteristics

Slides:



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

1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
The Web Warrior Guide to Web Design Technologies
Lesson 4: Formatting Input Data for Arithmetic
Lecture 6 8/11/11.
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
Introduction to scripting
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
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.
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.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
JS Basics 1 Lecture JavaScript - Basics. JS Basics 2 What is JavaScript JavaScript is a “simple”, interpreted, programming language with elementary object-
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
Overview of Form and Javascript fundamentals. Brief matching exercise 1. This is the software that allows a user to access and view HTML documents 2.
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.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
Jaana Holvikivi 1 Introduction to Javascript Jaana Holvikivi Metropolia.
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.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Document Object Model Nasrullah. DOM When a page is loaded,browser creates a Document Object Model of the Page.
Expressions and Data Types Professor Robin Burke.
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.
REEM ALMOTIRI Information Technology Department Majmaah University.
Introduction to.
Build in Objects In JavaScript, almost "everything" is an object.
Event-Driven Programming
Internet of the Past.
Chapter 6 JavaScript: Introduction to Scripting
Tutorial 10 Programming with JavaScript
Web Development & Design Foundations with HTML5
© 2017 Akhilesh Bajaj, All Rights Reserved
© 2015, Mike Murach & Associates, Inc.
Unit M Programming Web Pages with
Project 9 Creating Pop-up Windows, Adding Scrolling Messages, and Validating Forms.
Web Development & Design Foundations with HTML5 7th Edition
JavaScript Functions.
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
JavaScript.
14 A Brief Look at JavaScript and jQuery.
JavaScript Introduction
JavaScript an introduction.
Web Systems Development (CSC-215)
Objectives Insert a script element Write JavaScript comments
DHTML Javascript Internet Technology.
Understanding JavaScript and Coding Essentials
Event Driven Programming & User Defined Functions
WEB PROGRAMMING JavaScript.
DHTML Javascript Internet Technology.
The University of Tulsa
JavaScript What is JavaScript? What can JavaScript do?
JavaScript Basics Topics Overview Syntactic Characteristics
Tutorial 10 Programming with JavaScript
JavaScript What is JavaScript? What can JavaScript do?
Training & Development
JavaScript CS 4640 Programming Languages for Web Applications
HTML5 and Local Storage.
Tutorial 10: Programming with javascript
JavaScript Basics Topics Review Important Methods Writing Functions
Java Script Siddharth Srivastava.
JavaScript is a scripting language designed for Web pages by Netscape.
Introduction to Programming and JavaScript
Chapter 4 - JavaScript Events, Objects, & Functions
Web Programming and Design
JavaScript CS 4640 Programming Languages for Web Applications
Presentation transcript:

JavaScript Basics Topics Overview Syntactic Characteristics JavaScript - Practice Functions in JavaScript - Practice

Overview JavaScript is used to create interactive content. The objective behind the development of JavaScript is client-side development. JavaScript was originally developed by Netscape in the mid 1990s. It was originally called ActionScript. JavaScript is the main tool for create interactive content on the Internet.

JavaScript Variables Before a variable can be used, it must be declared. Variables are declared with the var keyword. var x; var y;

JavaScript is an untyped language The datatype of a variable is not required when it is defined. The datatype of a variable can change during the execution of a program. A variable can store the following data types: Numbers Strings Boolean JavaScript also defines two trivial data types: null undefined JavaScript supports a composite data type known as object.

JavaScript document document is the object that lets you work with the Document Object Model (DOM) that represents HTML elements of a page. document.open() will open/load a document for writing. document.write() will write to a document that has been loaded. Note: document.open() is not required if you’re using the html page for writing.

Practice 1 Show the output displayed for the following: var i = 25; var j = "25"; var k = 2 + "5"; var l = "2" + "5"; document.write(i == j); document.write(j == k); document.write(i == k); document.write(k == l);

Practice 1 Solution var i = 25; var j = "25"; var k = 2 + "5"; var l = "2" + "5"; document.write(i == j); true document.write(j == k); true document.write(i == k); true document.write(k == l); true

Practice 2 Locate errors: var words = 'These french fries are cold."; var isBad = "false"; var $fun = "Bobo went for swim."

Practice 2 Solution Delimit strings with two double quotes or two single quotes. Don’t mix them. var words = 'These french fries are cold."; var isBad = "false"; var $fun = "Bobo went for swim." Don’t put double quotes around a reserved word . It’s okay to use $ to begin a variable. Missing semicolon.

Practice 1: Building content with JavaScript Write the HTML, and JavaScript to produce the webpage shown below. Note: The body tag should be empty Use JavaScript to compute the date and time information Format date and time exactly as shown in the image See the next slide for the HTML The year is 2018 It is Tuesday: Day 30 of January in the year 2018 The time is 10:05am

Practice 1: 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.

Practice 1 navigation.js // TASK 1: GET THE COMPREHENSIVE DATE INFO var date = new Date(); // TASK 2: USE date TO COMPUTE TIME INFORMATION // TASK 3: USE date TO COMPUTE DAY, WEEKDAY, ETC. // TIP: Use a switch statement for specific values // TASK 4: OUTPUT CONTENT TO THE WEBPAGE.

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 they 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> <label> represents a caption for an item in the user interface. <input> is used to create an input control element in the user interface.

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; em vs px Pixels and points are static measurements. Percent and EMs on the other hand are relative measurements. The size of EM or percent-based text depends on its parent. Style sheets that use EMs are often easier to maintain for the designer and more accessible to end-users.

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>