IS2802 Introduction to Multimedia Applications for Business Lecture 3: JavaScript and Functions Rob Gleasure

Slides:



Advertisements
Similar presentations
1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.
Advertisements

The Web Warrior Guide to Web Design Technologies
Georgia Institute of Technology JavaScript part 2 Barb Ericson Georgia Institute of Technology May 2006.
Introduction to Web Site Development Steven Emory Department of Computer Science California State University, Los Angeles Lecture 8: JavaScript I.
Computer Science 103 Chapter 4 Advanced JavaScript.
JavaScript 101 Lesson 5: Introduction to Events. Lesson Topics Event driven programming Events and event handlers The onClick event handler for hyperlinks.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
Introduction to JavaScript. Aim To enable you to write you first JavaScript.
JS: DOM Form Form Object Form Object –The Form object represents an HTML form. –For each instance of a tag in an HTML document, a Form object is created.
Introduction to JavaScript. JavaScript Facts A scripting language - not compiled but interpreted line by line at run-time. Platform independent. It is.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
Bridges To Computing General Information: This document was created for use in the "Bridges to Computing" project of Brooklyn College. You are invited.
SYST Web Technologies SYST Web Technologies Lesson 6 – Intro to JavaScript.
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.
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
What is Java Script? An extension to HTML. An extension to HTML. Allows authors to incorporate some functionality in their web pages. (without using CGI.
G053 - Lecture 16 Validating Forms Mr C Johnston ICT Teacher
Client Side Programming with JavaScript Why use client side programming? Web sides built on CGI programs can rapidly become overly complicated to maintain,
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
1 JavaScript in Context. Server-Side Programming.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
CO1552 Web Application Development HTML Forms, Events and an introduction to JavaScript.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
Introduction.  The scripting language most often used for client-side web development.  Influenced by many programming languages, easier for nonprogrammers.
JavaScript - A Web Script Language Fred Durao
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (there is an audio component to this eLesson) © Dr.
JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.
Intro to JavaScript. Some simple examples Examples from our webpage Examples from Andrews webpage Today’s Example.
1 JavaScript
JavaScript - Basic Concepts Prepared and Presented by Hienvinh Nguyen, Afshin Tiraie.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
Event JavaScript's interaction with HTML is handled through events that occur when the user or browser manipulates a page. When the page loads, that is.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
JavaScript Introduction.  JavaScript is a scripting language  A scripting language is a lightweight programming language  A JavaScript can be inserted.
COMP403 Web Design JAVA SCRİPTS Tolgay KARANFİLLER.
ECA 225 Applied Interactive Programming ECA 225 Applied Online Programming control structures, events, objects.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (NON-Audio version) © Dr. David C. Gibbs WDMD.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Java Script About Java Script Document Object Model Incorporating JavaScript Adding JavaScript to HTML Embedding a Javascript External Scripts Javascript.
Pertemuan 5 IT133 Pengembangan Web JavaScript. What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting.
CIS 3.5 Lecture 2.3 "Introduction to JavaScript".
IS2802 Introduction to Multimedia Applications for Business Lecture 4: JavaScript, Loops, and Conditional Statements Rob Gleasure
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 2: Introduction to IS2803 Rob Gleasure
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
CGS 3066: Web Programming and Design Spring 2016 Introduction to JavaScript.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
Tarik Booker CS 120 California State University, Los Angeles.
Third lecture Event 27/2/2016 JavaScript Tutorial.
CHAPTER 10 JAVA SCRIPT.
Week 3: Introduction to Javascript
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Week 4: Introduction to Javascript
JavaScript (JS) Basics
Web Development & Design Foundations with HTML5 7th Edition
DHTML Javascript Internet Technology.
Your 1st Programming Assignment
Event Driven Programming & User Defined Functions
WEB PROGRAMMING JavaScript.
DHTML Javascript Internet Technology.
Tutorial 10: Programming with javascript
JavaScript: Introduction to Scripting
Web Programming and Design
Presentation transcript:

IS2802 Introduction to Multimedia Applications for Business Lecture 3: JavaScript and Functions Rob Gleasure

Lecture Outline JavaScript Functions Where to place JavaScript JavaScript events JavaScript alerts JavaScript code snippets to try out

JavaScript Functions A function is a collection of statements, grouped together under one name Upon completion of a function, a value is returned Each function will also accept certain arguments, i.e. different values upon which to operate their collection of statements A function remains dormant until the main program or another function ‘calls’ it.

JavaScript Statements There are a number of common types of JavaScript statements  Variable declarations  Function calls  Conditional Statements and loop Statements  Object Manipulation Statements  Comment Statements  Exception Handling Statements We’ll come back to these next week

Defining Functions JavaScript provides a range of predefined methods for use by developers, as well as allowing programmers to define their own functions Defining functions allows for  Greater reusability of code (saved time and effort)  Easier testing  Easier adaptation (code only has to be altered in one place)  Tidier/more logical code

Defining Functions Function definition syntax: function functionName(argument1, argument2, …) { //…Statements to be executed… } The body of the function must be enclosed in curly braces

Defining Functions Definition begins with the function keyword Following this is the name of the function  As with variable names, the function name must start with a letter or an underscore and may not contain spaces  The name is case-sensitive and must be unique but may contain any combination of alphanumeric characters Every function name includes a set of parentheses  These parentheses contain the arguments

Defining Functions A function requires a specific number of arguments when it is called (although this number may often be zero) Arguments are variables used in the function. These variables don’t need to be explicitly declared and are assigned a value when the function is called Example: function displayAlert(value) { If(value == true){ alert(“The value is true”); //function call }

Defining Functions The return statement Used to return control to the point at which the function was called Usually used if the function returns a result:  return true; May also be used if the function does not return a result:  return; Note: some people consider the second use of the return statement to be symptomatic of sloppy programming…

The Location of JavaScript There are three general areas that JavaScript can be placed for use in a webpage. 1. Inside the head tag 2. Within the body tag 3. In an external file The location of the JavaScript depends on when you wish the code to be executed, as well as other factors we will return to later

The Location of JavaScript If you want to have a script run on some event, e.g. when a user clicks somewhere  place that script in the head. If you want the script to run when the page loads, e.g. because you wish to load up different variants of the page depending upon the user’s browser  place the script within the body tag. If you want the script to run in several different pages  place the script within an external.js file

The Location of JavaScript Save the following code in a new file called lecture_two_demo.html function addArguments(a,b) { return a + b; } document.write(addArguments(4,3));

JavaScript Events Events allow JavaScript pages to be dynamic and interactive Example of events include:  A mouse click  The webpage loading  An image loading  Mouse moving over a hot spot on the webpage (usually referred to as hovering)  Submitting a HTML form  A keystroke  Timed events

JavaScript Events These events have predefined names, e.g. ‘onclick’, ‘onmouseover’, ‘onmouseout’  A complete list can be found at To cause something to happen when an event occurs, we must: 1. Specify the event 2. Specify the HTML element that will be waiting for the event to occur 3. Specify what functions will be called when the event occurs

JavaScript Alerts An alerts is a pop-up dialogue box, usually placed ‘in front of’ the user’s browser window Alerts usually require some sort of action to be taken by the user before they can resume browsing, e.g. clicking ‘ok’, ‘cancel’, etc Alerts are annoying…

JavaScript Alerts So when do we use alerts?  When it’s very important that a user sees a message, e.g. when a security risk may be incurred by continuing to browse  When we wish to warn a user of content, e.g. if offensive humour is present  When some problem has occurred which means the website will not be able to function properly  When you need active confirmation for something, e.g. ‘can we you with our information newsletter?’ (told you they were annoying) Alerts must be used sparingly, “Die pop-up die!!” is not an uncommon reaction to them among users

JavaScript Confirmation Pop-ups A confirm function provides the user with a choice about whether to agree to a certain action or not They are often used when an action is important, e.g. when a user is being transferred to a new sight or submitting details of payment

JavaScript Code Snippets to Try Out Copy this code into Notepad, saving as lecture_2_Ex.html (continued onto next page) <!-- function annoy() { alert("You clicked!") } function harass() { alert("You hovered!") } function confirmation() { var answer = confirm(“You’re leaving already?") if (answer){ alert(“Fine then go, what do I care!") window.location = " } } //-->

JavaScript Code Snippets to Try Out Hover over this

Exercise We’re going to make a button in lecture_2_Ex.html that tells us how many times it has been clicked  First, we need to create a variable called count with an initial value of 0  Make a new button on the page with the text “Count clicks” as a label  Define a new function in lecture_2_Ex.html l called alertCount() and set the button we just made to call this function when clicked  We want alertCount() to do two things Increment count (i.e. ad one to it) Create an alert with the value count

Exercise (cont.) Create a new button with the text “Reset count” and set it to call a function called resetCount() when clicked Define this resetCount() function, such that calling it sets count to zero Things to consider  Where do we declare our variable count?  What happens if we declare it inside the alertCount() function?  What happens when we refresh the page?

References and Further Reading l