Session 6. JavaScript Part 1. Reading

Similar documents
Session 16. JavaScript Part 1. Reading

The first sample. What is JavaScript?

Q1. What is JavaScript?

<form>. input elements. </form>

Introduction to JavaScript

Web Programming/Scripting: JavaScript

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

CGS 3066: Spring 2015 JavaScript Reference

Lecture 14. Introduction to JavaScript. Mr. Mubashir Ali Lecturer (Dept. of Computer Science)

JavaScript s role on the Web

CISC 1600 Lecture 2.4 Introduction to JavaScript

CISH-6510 Web Application Design and Development. Overview of JavaScript. Overview

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

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

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

JavaScript: Getting Started

Javascript Lecture 23

JavaScript Introduction

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

JavaScript CS 4640 Programming Languages for Web Applications

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

New Perspectives on Creating Web Pages with HTML. Tutorial Objectives


JavaScript CS 4640 Programming Languages for Web Applications

This course is designed for web developers that want to learn HTML5, CSS3, JavaScript and jquery.

Session 11. Ajax. Reading & Reference

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

JavaScript Introduction

CSS The web browser uses its own resources, and eases the burden on the server. It has fewer features than server side scripting.

LECTURE-2. Functions review HTML Forms. Arrays Exceptions Events. CS3101: Scripting Languages: Javascript Ramana Isukapalli

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

Place User-Defined Functions in the HEAD Section

Web Development & Design Foundations with HTML5, 8 th Edition Instructor Materials Chapter 14 Test Bank

Documents and computation. Introduction to JavaScript. JavaScript vs. Java Applet. Myths. JavaScript. Standard

Client vs Server Scripting

By the end of this section of the practical, the students should be able to:

Internet Technologies 5-Dynamic Web. F. Ricci 2010/2011

Fundamentals of Website Development

Client Side JavaScript and AJAX

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

By the end of this section of the practical, the students should be able to:

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

CITS1231 Web Technologies. JavaScript

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

(Refer Slide Time: 01:40)

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

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

Javascript. Many examples from Kyle Simpson: Scope and Closures

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

Javascript. UNIVERSITY OF MASSACHUSETTS AMHERST CMPSCI 120 Fall 2010

INF5750. Introduction to JavaScript and Node.js

1. Cascading Style Sheet and JavaScript

Just add an HTML comment tag <!-- before the first JavaScript statement, and an end-of comment tag --> after the last JavaScript statement, like this:

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

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

CSC Web Programming. Introduction to JavaScript

Session 7. JavaScript Part 2. W3C DOM Reading and Reference

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2017)

JavaScript: Introductionto Scripting

5/19/2015. Objectives. JavaScript, Sixth Edition. Introduction to the World Wide Web (cont d.) Introduction to the World Wide Web

JavaScript Handling Events Page 1

GRITS AJAX & GWT. Trey Roby. GRITS 5/14/09 Roby - 1

Photo from DOM

Week 13 Thursday (with Page 5 corrections)

Task 1. Set up Coursework/Examination Weights

Web Site Development with HTML/JavaScrip

Key features. Nothing to do with java It is the Client-side scripting language Designed to add interactivity to HTML pages

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

Controlled Assessment Task. Question 1 - Describe how this HTML code produces the form displayed in the browser.

When learning coding, be brave

New Media Production Lecture 7 Javascript

CSC Javascript

Working with JavaScript

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?

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

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

PES DEGREE COLLEGE BANGALORE SOUTH CAMPUS 1 K.M. before Electronic City, Bangalore WEB PROGRAMMING Solution Set II

Introduction to DHTML

Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world

1$ 5 ! $ 6 4 * Source: 0 "!*! 0! * 8 97 ?!$ 5 0 *! 4! $ 0 : * ' () 7;7 7<7

JavaScript. Asst. Prof. Dr. Kanda Saikaew Dept. of Computer Engineering Khon Kaen University

Manju Muralidharan Priya. CS4PM Web Aesthetics and Development WEEK 11

5. JavaScript Basics

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

8/19/2018. Web Development & Design Foundations with HTML5. Learning Objectives (1 of 2) Learning Objectives (2 of 2) What is JavaScript?

Tutorial 10: Programming with JavaScript

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

Notes on JavaScript (aka ECMAScript) and the DOM

jquery and AJAX

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

Govt. of Karnataka, Department of Technical Education Diploma in Computer Science & Engineering. Fifth Semester. Subject: Web Programming

CS Final Exam Review Suggestions - Spring 2018

COMS 469: Interactive Media II

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

Documents and computation. Introduction to JavaScript. JavaScript vs. Java Applet. Myths. Standard

PHP & My SQL Duration-4-6 Months

The Web: Concepts and Technology

CHAPTER 6 JAVASCRIPT PART 1

A Balanced Introduction to Computer Science, 3/E

Part of this connection identifies how the response can / should be provided to the client code via the use of a callback routine.

Transcription:

Session 6 JavaScript Part 1 Reading Reading Wikipedia en.wikipedia.org/wiki/javascript Web Developers Notes www.webdevelopersnotes.com/tutorials/javascript/ JavaScript Debugging www.w3schools.com/js/js_debugging.asp jquery-dom http://www.ibm.com/developerworks/xml/tutorials/xprocessxmljquerytut/index.html We cover jquery later in the course 2 9/18/2018 1

References Reference ECMAScript www.ecma-international.org/publications/files/ecma-st/ecma-262.pdf Mozilla Guide https://developer.mozilla.org/en-us/docs/web/javascript/reference W3C www.w3.org/tr/rec-html40/interact/scripts.html 3 Reference Book JavaScript: The Definitive Guide by David Flanagan, O Reilly Press, 6 th Edition (might be available through Safari Books On-Line and Google Books) the only complete JavaScript book I have seen that is written from a CS perspective 4 9/18/2018 2

Background Reading Also possibly available through CS Library using Safari Books On-line Head First HTML5 Programming: Building Web Apps with JavaScript by Eric Freeman and Elisabeth Robson, O Reilly Press XML In a Nutshell, Chapter 19 (DOM) Not written for CS majors, but reasonably correct 5 Learning Goals Understand differences of JavaScript as compared with Java Understand syntactic and semantic structure of JavaScript Understand use of events 6 9/18/2018 3

What is JavaScript? A scripting language (i.e., a lightweight programming language) to use within a browser Usually embedded directly into HTML pages The sole surviving language for Web client programming An interpreted language (means that scripts execute without preliminary compilation) The name officially refers to ECMAScript 7 Useful for Why Should You Learn JavaScript Client side form processing (e.g., field validation) More dynamic graphic UI Dynamic update of html pages - Ajax 8 9/18/2018 4

Ajax JavaScript is essential to the use of Ajax Ajax provides a new Web interaction style Examples: maps.google.com/maps nyc.bestparking.com/ With an Ajax application, parts (but not all) of a page will change based on user input 9 Important Concepts Low-level syntax of JavaScript is similar to Java, but the object model is very different A JavaScript can be set to respond to GUI events JavaScript treats functions as first class objects (you can use them in places where you would use other objects) JavaScript is a weakly typed language (implicit type conversion) Browsers provide access to the document tree with JavaScript using the Document Object Model (DOM) JavaScript code can request data from the server for update of the document tree (and browser update of the page) The Browser Object Model (BOM) forms a hierarchy of objects that interact with the browser 10 9/18/2018 5

Hello JS <html> <body> <h2> <script> document.write("hello World!"); </script> </h2> </body> </html> Script tag is used to insert JavaScript into a page Semicolon is optional (but mandatory for multiple statements on a line) Code within a script element is executed immediately when the page is loaded (if it is not in a function) 11 JavaScript Development Major browsers have JavaScript debuggers available Firefox More on JavaScript debugging once we cover libraries Chrome NetBeans has good syntax analysis features Be careful in debugging sometimes a JavaScript function will just return if it encounters an error 12 9/18/2018 6

JavaScript Object Notation Object name Method invocation String literal document.write("hello World!"); Syntax similar to Java But what is the document object? 13 Window as Global Execution Context The document object represents the html document The window object represents the browser window that displays the document The window object is the global object (think of it as the default object) The document object is a property of the window object window.document.write( ) document.write( ) Is similar to this.getservletcontext( ) getservletcontext( ) 14 9/18/2018 7

Possible values Script Tag Type Attribute <script type= text/javascript> text/javascript text/ecmascript A standard version of Javascript text/jscript Microsoft s version of Javascript text/vbscript Runs only in IE text/vbs text/xml With HTML 5, all you need is <script> 15 JavaScript Functions Scripts that appear in the head element of the document are loaded first A non-function script in the head element will execute before the page loads (not too useful) A function defined in the head element will be loaded before anyone uses it, and so is available to any function call in JavaScript located in body 16 9/18/2018 8

Syntax var strname = x or JavaScript Variables strname = x Variables declared within a function are local to the function Variables declared outside a function are properties of the window object (visible everywhere in the page) 17 Operations Arithmetic Assignment Comparison Logical String Conditional Syntax is very similar to Java (both are based on C) All Java keywords are reserved in JavaScript 18 9/18/2018 9

Popup Boxes Alert box user has to click OK to proceed Confirm box user has to either click OK or cancel to proceed Prompt box user enters a value, then clicks either OK or Cancel to proceed Methods of the Window object alert( Email must be filled out ); confirm( sometext ); prompt( sometext, defaultvalue ); Popup boxes act as breakpoints for debugging 19 If, else statements Conditional Statements End of statement semicolons can be omitted if each statement is on a separate line <script> //If the time is less than 10, //you will get a "Good morning" greeting. //Otherwise you will get a "Good day" greeting. var d = new Date() var time = d.gethours() if (time < 10) { document.write("good morning!") } else { document.write("good day!") } </script> 20 9/18/2018 10

Similar to style sheets External JavaScripts Script can either be embedded or referenced in an external file <html> <head> <script src="xxx.js ></script> </head> <body> </body> </html> 21 Functions To keep the browser from executing a script as soon as the page is loaded, write your script as a function. A function contains some code that will be executed only by an event or by a call to that function. You may call a function from anywhere within the page Functions are defined at the beginning of a page, in the <head> section (so that they are available when your page begins to load) 22 9/18/2018 11

Function Example <html> <head> <script> function displaymessage() { alert("hello World!") } </script> </head> <body> <form> <input type="button" value="click me!" onclick="displaymessage()" > </form> </body> </html> 23 Events Events are actions that can be detected by JavaScript Elements on a Web page have events that can be used to execute JavaScript functions Examples Mouse click Image load Mouse over Form submittal <input type="button value="click me! onclick="displaymessage()" > Notice that these event attributes are not camel case 24 9/18/2018 12

Typical Event Handlers onclick - when the pointing device button is clicked over an element onmouseover - when the pointing device is moved onto an element onchange - when a control loses the input focus and its value has been modified since gaining focus onblur - when an element loses focus either by the pointing device or by tabbing navigation onfocus - when an element receives focus either by the pointing device or by tabbing navigation onsubmit when the submit button of a form element is clicked 25 Are We on Track Write an html page that contains a form with a text box and a submit button When the text box loses focus (after you enter text and hit tab) display an alert box with the text CSE336 Value of action attribute in form tag does not matter 26 9/18/2018 13

Were We on Track? <script> function f() { alert( CSE336"); } </script> </head> <body> <h1>track - JavaScript Example</h1> <form action="http://localhost:8080/codecse336/jsps/formtester3.jsp" method="post" > <input type="text" name="js-input" value="enter text" onblur="f();" /> <br /><input type="submit" /> </form></body></html> 27 Guidelines White space is ignored Case sensitive Comments (// ) 28 9/18/2018 14

Objects Object properties are accessed with the dot (.) operator Object encapsulation practice is not like Java Object methods invoked with the dot (.) operator and a parameter list ( () ) Built-in objects Objects are really maps, where the map value can be a function String var mydate=new Date() Date mydate.setfullyear(2010,0,14) Array Boolean var mycars=new Array() mycars[0]="saab mycars[1]="volvo mycars[2]="bmw" 29 Have You Satisfied the Lecture Objectives? Understand differences of JavaScript as compared with Java Understand syntactic and semantic structure of JavaScript Understand use of events 30 9/18/2018 15