JavaScript Print slides by Sean Boyle, with overall content based on material provided by Dan Bogaard, from: http://people.rit.edu/dsbics/536/lecture/jsBasics.html.

Slides:



Advertisements
Similar presentations
Chapter 08: Adding Adding Interactivity Interactivity With With Behaviors Behaviors By Bill Bennett Associate Professor MSJC CIS MVC.
Advertisements

JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
Introduction to JavaScript Module 1 Client Side JS Programming M-GO Sponsored By
Chapter 7 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 7 Sebesta: Programming the World Wide Web.
20-Jun-15 JavaScript and HTML Simple Event Handling.
JavaScript Client Side scripting. Client-side Scripts Client-side scripts, which run on the user’s workstation can be used to: Validate user inputs entered.
Introduction To JavaScript What JavaScript Is: a general purpose scripting language that allows a static page to interact with users and respond to events.
UNIT 6 JAVASCRIPT EVENT HANDLERS &WEB BROWSERS DETECTION.
HTML Forms/Events (Instructor Lesson) The Event model HTML Forms Custom Events 1.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Event and Event Handling.
JavaScript - a Brief Introduction Anupriya. What is JavaScript Object based (not object oriented) programming language –very limited object creation –a.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Chapter 19: Adding JavaScript
JavaScript II ECT 270 Robin Burke. Outline JavaScript review Processing Syntax Events and event handling Form validation.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
Internet Mark-up Languages CO32036 Part Lecture: Elementary JavaScript.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
DOM and JavaScript Aryo Pinandito.
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.
Javascript. Outline Introduction Fundamental of JavaScript Javascript events management DOM and Dynamic HTML (DHTML)
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
Intro to JavaScript Scripting language –ECMAScript compliant –Client side vs. server side Syntactic rules –White space –Statement end: ; optional –Comments:
JavaScript Syntax, how to use it in a HTML document
JavaScript - Basic Concepts Prepared and Presented by Hienvinh Nguyen, Afshin Tiraie.
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.
By Tharith Sriv. To write a web page you use: HHTML (HyperText Markup Language), AASP (Active Server Page), PPHP (HyperText Preprocessor), JJavaScript,
Project 8: Reacting to Events Essentials for Design JavaScript Level One Michael Brooks.
COS 125 DAY 20. Agenda Assignment 8 not corrected yet Assignment 9 posted  Due April 16 New course time line Discussion on Scripts 
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Web Programming Java Script & jQuery Web Programming.
CIS 3.5 Lecture 2.3 "Introduction to JavaScript".
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
7. JavaScript Events. 2 Motto: Do you think I can listen all day to such stuff? –Lewis Carroll.
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 II ECT 270 Robin Burke. Outline Functions Events and event handling Form validation.
LESSON : EVENTS AND FORM VALIDATION -JAVASCRIPT. EVENTS CLICK.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
CGS 3066: Web Programming and Design Spring 2017
Programming With JavaScript
JavaScript and HTML Simple Event Handling 11-May-18.
Programming Web Pages with JavaScript
In this session, you will learn to:
Web Development & Design Foundations with HTML5 7th Edition
JAVASCRIPTS AND HTML DOCUMENTS
14 A Brief Look at JavaScript and jQuery.
JavaScript and HTML Simple Event Handling 19-Sep-18.
JavaScript Events.
JavaScript Introduction
JavaScript an introduction.
DHTML Javascript Internet Technology.
Your 1st Programming Assignment
DHTML Javascript Internet Technology.
CHAPTER 7 JavaScripts & HTML Documents
JavaScript Defined General Syntax Body vs. Head Variables Math & Logic
JavaScript CS 4640 Programming Languages for Web Applications
JavaScript Intro.
JavaScript and Ajax (JavaScript Events)
CNIT 133 Interactive Web Pags – JavaScript and AJAX
JAVA SCRIPT OBJECTS & DOM
Web Programming and Design
Web Programming and Design
Web Programming and Design
JavaScript and HTML Simple Event Handling 26-Aug-19.
JavaScript CS 4640 Programming Languages for Web Applications
JavaScript and HTML Simple Event Handling 4-Oct-19.
Presentation transcript:

JavaScript Print slides by Sean Boyle, with overall content based on material provided by Dan Bogaard, from: http://people.rit.edu/dsbics/536/lecture/jsBasics.html Additional material by Charlie Roberts 1

JavaScript What is it? NOT Java Based on ECMAScript Light-weight language (compiled V8, SpiderMonkey, Chakra) Object-oriented Client-side and server-side via node.js application NOT Java LiveScript JavaScript JScript Based on ECMAScript Open-source “universal” language

JavaScript Prototype-based inheritance model (based on Self) Any object can inherit from any other object Different from C++/Java which use abstract classes. Functional Functions are first-class values, can be passed as arguments to functions and returned from calls to functions. They’re also objects! Syntax is based on Java (part of the reason for the name “JavaScript”) which in turn is based on C++

JavaScript ES5 – first standardized in 2009, now supported in almost all browsers. Only contains functional scoping (more later). ES6 / ES2015 – Mostly supported, but only Safari (!!!) has 100% support. Enables both block and functional scoping and in general is much, much, nicer. http://kangax.github.io/compat-table/es6/

JavaScript How do I know it will work? Implemented in all major browsers …but not implemented the same http://caniuse.com/

JavaScript Basics Placement Between a pair of <script> and </script> tags in body or head. //comments, code and functions go here! var x=5; myFunct(x); </script> From an external file specified by the src attribute of <script> tag <script type="text/javascript" src="../myScript.js”/>

JavaScript Basics Placement In an event handler, specified as the value of an HTML attribute such as onclick or onmouseover <a href="#" onclick="window.location='http://www.rit.edu’; alert(myFunct())">click to make it happen</a> As the body of a URL that uses the special javascript: protocol. <a href="javascript: window.location='http://www.rit.edu'; alert(myFunct())">click to make it happen</a>

JavaScript Basics Comments: Troubleshooting //This is a single line comment /*This is a multiple line comment*/ Troubleshooting Firefox: Go Tools > Web Developer > Web Console, or use Firebug Chrome: Right-click > Inspect Element > Console 8

Programming Refresher Variables (type, scope, declaration) JavaScript variables are un-typed Scope is very important. Assuming we’re using ES5, all scope of variables is determined by the function they’re created in. If they’re not created inside of a function, then they are global. Conditionals (if, switch) Loops (for, for...in, while, do...while) Functions Discuss the concept of scope 9

JS Objects Everything is an object! Window Document Navigator elements Properties - Object Descriptions Name Length Source Value Methods - Object Actions write() open() cloneNode() RemoveEventListener()

Document Object Model

DOM Everything in HTML is a box Everything also is uniquely identified by ID (we know how to do this, right?) JavaScript can access any element within this structure, by its name or ID…

JavaScript Basics Use document.querySelector to find element via CSS selector: // find by id attribute myBtn = document.querySelector(‘#myBtn’) // find by class myBtn2 = document.querySelector(‘.myBtn2’) // find by tag name header = document.querySelector(‘h1’)

JS Events Object 'Triggers': onload onunload onclick onmouseover onmouseout onmousemove onmousedown onmouseup onmove onresize onchange onsubmit onreset onresize onabort onblur onfocus ondblclick ondragdrop onerror onkeydown onkeypress onkeyup …and more! These are events that we respond to via EVENT LISTENERS 14

JS Events JavaScript Event vs. HTML attribute HTML attribute events are case in-sensitive (onmouseover or onMouseOver) <img src="pic.jpg" onmouseover="jsFunct();"> - OK! <img src="pic.jpg" onMouseOver="jsFunct();"> - OK! JS event (written between <script> tags) MUST be lower case (onmouseover) objName.onmouseover=functToHappen; - OK! objName.onMouseOver=functToHappen; - Will Break! JS is Case Sensitive! (var x is a different variable than var X)

Functions Why functions? Convenient, cleaner code, re-usability, can make execute only when we desire! Function creation must use the function keyword with parentheses and { } function myFunct(arg1, arg2){ } Function calls simply put the name of the function in the code with ( ) (that is 2 parentheses) myFunct(); myFunct(arg1, arg2); i.e. Methods 16

Functions return can be used to return a value, or jump out of a function early Useful (pre-built) functions: setTimeout() setInterval() eval() parseInt() typeof() Math.random() Math.floor() blur() focus()

Arrays Simple array construction (all of the following are same, but you should only use the last option): var newArr = new Array(3); newArr[0]=”me"; newArr[1]=34; newArr[2]=6.1; var newArr=new Array(”me",34,6.1); var newArr = [”me", 34, 6.1];

Arrays Ordered set of indexed elements... Indexed by numbers document.images[0].width - Index the array with a number Arrays are objects, so they can also have properties: console.log( ‘length is:’, myArray.length ) While document.querySelector returns a single item, document.querySelectorAll will return an array of all items matching a CSS selector.

So what? Behavior “layer” (remember those?) Rollovers (mostly done with CSS these days) Form processing and validation Web apps Games http://www.chromeexperiments.com/tag/not-webgl/ We will be scratching the surface with JavaScript, but there’s a LOT more it can do. Note 330, games in JS. 20

Demo Let’s make some rollovers!