CISC 1600 Lecture 2.4 Introduction to JavaScript

Similar documents
CISC 1600 Lecture 2.4 Introduction to JavaScript

Outline. Introduction to JavaScript Resources What is JavaScript? JavaScript in web pages

What is Java Script? Writing to The HTML Document. What Can JavaScript do? CMPT 165: Java Script

Introduction to JavaScript

CHAPTER 6 JAVASCRIPT PART 1

The first sample. What is JavaScript?

Working with JavaScript

Session 16. JavaScript Part 1. Reading

Interactor Tree. Edith Law & Mike Terry

Session 6. JavaScript Part 1. Reading

(Refer Slide Time: 01:40)

Q1. What is JavaScript?

<form>. input elements. </form>

JavaScript s role on the Web

JavaScript is described in detail in many books on the subject, and there is excellent tutorial material at

Software. Programming Languages. Types of Software. Types of Languages. Types of Programming. Software does something

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

Web Programming/Scripting: JavaScript

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

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

CITS1231 Web Technologies. JavaScript

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2017)

JavaScript: Introduction, Types

CISC 1600 Lecture 2.4 Game design. Topics: The business of Games Funativity Concrete components Narrative Mechanics, Dynamics, & Aesthetics

COMP519 Web Programming Lecture 16: JavaScript (Part 7) Handouts

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?

JavaScript: the language of browser interactions. Claudia Hauff TI1506: Web and Database Technology

CS 110 Exam 2 Spring 2011

CS50 Quiz Review. November 13, 2017

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

Place User-Defined Functions in the HEAD Section

CITS3403 Agile Web Development Semester 1, 2018

COURSE OUTLINE MOC 20480: PROGRAMMING IN HTML5 WITH JAVASCRIPT AND CSS3

Programming language components

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

CGS 3066: Spring 2015 JavaScript Reference

5. JavaScript Basics

PIC 40A. Midterm 1 Review

Lecture 3: The Basics of JavaScript. Background. Needs for Programming Capability. Origin of JavaScript. Using Client-side JavaScript

JavaScript CS 4640 Programming Languages for Web Applications

UNIT -II. Language-History and Versions Introduction JavaScript in Perspective-

Princess Nourah bint Abdulrahman University. Computer Sciences Department

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

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

React. HTML code is made up of tags. In the example below, <head> is an opening tag and </head> is the matching closing tag.

JavaScript CS 4640 Programming Languages for Web Applications

EVENT-DRIVEN PROGRAMMING

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

COP 1220 Introduction to Programming in C++ Course Justification

A Balanced Introduction to Computer Science, 3/E

Events: another simple example


JavaScript code is inserted between tags, just like normal HTML tags:

JavaScript Specialist v2.0 Exam 1D0-735

Command-driven, event-driven, and web-based software

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

EXERCISE: Introduction to client side JavaScript

Data Abstraction and Specification of ADTs

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

Introduction to JavaScript p. 1 JavaScript Myths p. 2 Versions of JavaScript p. 2 Client-Side JavaScript p. 3 JavaScript in Other Contexts p.

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

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

A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University 2011 Pearson Prentice Hall ISBN

CS312: Programming Languages. Lecture 21: JavaScript

<script type="text/javascript"> script commands </script>

CS1046 Lab 4. Timing: This lab should take you 85 to 130 minutes. Objectives: By the end of this lab you should be able to:

Why Discuss JavaScript? CS312: Programming Languages. Lecture 21: JavaScript. JavaScript Target. What s a Scripting Language?

SEEM4570 System Design and Implementation Lecture 03 JavaScript

Object Oriented Methods : Deeper Look Lecture Three

Web Site Development with HTML/JavaScrip

Random number generation, Math object and methods, HTML elements in motion, setinterval and clearinterval

What is PHP? [1] Figure 1 [1]

Manju Muralidharan Priya. CS4PM Web Aesthetics and Development WEEK 11

Tutorial 10: Programming with JavaScript

JavaScript Programming

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

JavaScript Fundamentals_

A340 Laboratory Session #5

Level 3 Computing Year 2 Lecturer: Phil Smith

ITS331 Information Technology I Laboratory

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

Webinar. The Lighthouse Studio Scripting Series. JavaScript Sawtooth Software, Inc.

CS1 Lecture 3 Jan. 22, 2018

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

CSC Web Programming. Introduction to JavaScript

IN Development in Platform Ecosystems Lecture 2: HTML, CSS, JavaScript

Javascript. Daniel Zappala. CS 360 Internet Programming Brigham Young University

JavaScript and Ajax.

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

COP 1170 Introduction to Computer Programming using Visual Basic

Lecture 8 (7.5?): Javascript

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

COMS 469: Interactive Media II

This tutorial has been prepared for beginners to help them understand the basic functionalities of Gulp.

INTRODUCTION TO JAVASCRIPT

Client vs Server Scripting

Midterm Exam. 5. What is the character - (minus) used for in JavaScript? Give as many answers as you can.

JAVASCRIPT - CREATING A TOC

Transcription:

CISC 1600 Lecture 2.4 Introduction to JavaScript Topics: Javascript overview The DOM Variables and objects Selection and Repetition Functions A simple animation

What is JavaScript? JavaScript is not Java JavaScript was designed to add interactivity to HTML pages It has grown beyond that to run outside the browser JavaScript is usually interpreted but Google created an open source compiler for it (V8) JavaScript is a web standard

What can JavaScript do? Insert dynamic text into an HTML page Read and write HTML elements React to events: mouse, keyboard, network, etc. Asynchronously load or update parts of pages Validate form data before it is submitted Examples for class here

Lab 2.4, Part 1: Getting setup

Popup boxes JavaScript allows interactivity through the use of pop-up boxes There are 3 general kinds: alert("sometext"); confirm("sometext"); // returns "true" or "false") prompt("sometext","defaultvalue"); //returns what user enters as answer See 6_PopUps

JavaScript general syntax Syntax is much like Processing (or C or Java) Statements should end with a semicolon ; White space does not matter (except for line breaks sometimes) /*************************** This is JavaScript (JS), the programming language that powers the web (and this is a comment, which you can delete). ***************************/ function greetme(name) { } var today = new Date(); alert("hello " + name + ", today is " + today.todatestring());

JavaScript can be defined and used in the body or the head of the page <html> <head> <script type="text/javascript"> function message() { alert("this alert box called by onload event"); } </script> </head> <body onload="message()"> <script type="text/javascript"> document.write("message written by JavaScript"); </script> </body> </html> <!-- See 2_EventListeners-->

Interactivity: events and listeners JavaScript code can respond to events Events happen in response to User-interface interactions (mouse, keyboard) Network operations (page or resource loading) Can register event listener functions Called when event happens with information on event <button type="button" onclick="go()">go!</button>

Recall: Document Object Model (DOM) A web browser interprets your HTML And builds a model of the page, the DOM The DOM is what is rendered to the screen The DOM can be manipulated by CSS and JavaScript after it is built When building a page, consider its structure first, i.e., the DOM

Example DOM

Example DOM <html> <body> <h1> <p> Text Text

Lab 2.4, Part 2 Manipulating the DOM

JavaScript can manipulate the DOM Several ways to select DOM elements document.getelementbyid( idofelement ); document.getelementsbytagname( div ); parent.children[index]; Etc. Several ways to modify DOM elements element.innerhtml = Some HTML ; img.src = newurl ; element.style.border = 1pt solid ;

Simple DOM manipulation <html> <head> </head> <body> <script type="text/javascript"> document.write("<h1>hello World! </h1>"); </script> </body> </html> <!-- Note use of document object model -> <!-- Example Here: 1_FirstJavaScript.html -->

Variables hold values Variables are named locations for storing data Assign a value to a variable to refer to it later Values can be simple: number, string, boolean Or complex: DOM node, document object, array of other values Create a variable: var count; Assign a value to it: count = 0; Use it: newh1 =...count = + count + </h1> ; JavaScript in the browser has several pre-defined variables, e.g., window: the browser window/frame around the page document: the DOM and other page characteristics

Some JavaScript syntax Mathematical: +, -, *, /, %, ++, -- Logical: &&,,!, <, >, etc... Variable declaration and assignment: var x=10; var y=2; var z=x/y; for math, logic, etc. Strings can be concatenated with the '+' sign. var txt1 = "What a very"; var txt2 = "nice day"; var txt3 = txt1 + " " + txt2; Note: If you add a number and a string, the result will be a string!

JavaScript is object-oriented Like Processing, you can define new data types in JavaScript as objects An object is a collection of facts and functions Facts: data, the state of the object Functions: the available operations to perform on it In JavaScript, both are accessed using a dot '.' Fact: document.children Function: document.getelementbyid() An object can contain simple values or other objects

Lab 2.4, Part 3 Selection and repetition

Selection: The ability to make a choice Execute code only if certain conditions are true This is mainly the if statement in languages if ((count % 3) == 0) { target.innerhtml = target.innerhtml + newh1; } else { } target.innerhtml = newh1;

Repetition: The ability to repeat an action Repeatedly execute code, modifying state In JavaScript: mainly for and while loops var stars = ""; for (var i=0; i < count; i++) { stars = stars + "*"; } // OR while (stars.length < count) { } stars = stars + "*";

Lab 2.4, Part 4 Use functions to avoid repeating yourself

Functions: reuse operations JavaScript implements the procedural paradigm Because you can write and use procedures (functions) Functions take input arguments & return outputs function replaceinnerhtml(nodeid, html) { } var target = document.getelementbyid(nodeid); target.innerhtml = html; return target; Functions allow code to be more modular and reusable Debug a function once, use it many times

Lab 2.4, Part 5 Store multiple pieces of data in an array

Arrays are data structures that store multiple values Arrays in JavaScript are like arrays in Processing var langs = ['c', 'c++', 'java']; for (var i=0; i < 3; i++) { } console.log(langs[i]); Unlike Processing you can put things with different types into the same array in JavaScript var misc = [1, 'a', true]; for (var i=0; i < 3; i++) { } console.log(misc[i]);

Lab 2.4, Part 6 Create a simple animation

Lab 2.4, Part 7 Extensions