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

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

JAVASCRIPT BASICS. Handling Events In JavaScript. In programing, event-driven programming could be a programming

Princeton University COS 333: Advanced Programming Techniques A Subset of JavaScript

Princeton University COS 333: Advanced Programming Techniques A Subset of JavaScript

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2017)

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

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

Fundamentals of Website Development

Events: another simple example

New Perspectives on Creating Web Pages with HTML. Tutorial Objectives

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

Introduction to DHTML

710 Index Attributes, 127 action attribute, 263 assigning, bottom attribute, domain name attribute, 481 expiration date attribute, 480 8

HTML User Interface Controls. Interactive HTML user interfaces. Document Object Model (DOM)

CSC Javascript

Want to add cool effects like rollovers and pop-up windows?

Place User-Defined Functions in the HEAD Section

JavaScript Handling Events Page 1

Full file at Tutorial 2: Working with Operators and Expressions

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

Beijing , China. Keywords: Web system, XSS vulnerability, Filtering mechanisms, Vulnerability scanning.

Web Engineering (Lecture 06) JavaScript part 2

DOM Primer Part 2. Contents

Such JavaScript Very Wow

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

The first sample. What is JavaScript?

JSF - H:INPUTSECRET. Class name of a validator that s created and attached to a component

INTRODUCTION TO WEB DEVELOPMENT AND HTML. Lecture 15: JavaScript loops, Objects, Events - Spring 2011

Canvas & Brush Reference. Source: stock.xchng, Maarten Uilenbroek

Coding in JavaScript functions

3Lesson 3: Functions, Methods and Events in JavaScript Objectives

link document.getelementbyid("coffee").style.borderwidth = "0px" document.getelementbyid("tea").style.borderwidth = "10px"

JavaScript Programming Chris Seddon

CSC Web Programming. Introduction to JavaScript

Note: Java and JavaScript are two completely different languages in both concept and design!

Javascript. A key was pressed OR released. A key was released. A mouse button was pressed.

Photo from DOM

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

New Media Production Lecture 7 Javascript

Introduction to JavaScript

Installation and Configuration Manual

Q1. What is JavaScript?

Session 16. JavaScript Part 1. Reading

Name Related Elements Type Default Depr. DTD Comment

JS Tutorial 3: InnerHTML Note: this part is in last week s tutorial as well, but will be included in this week s lab

Session 6. JavaScript Part 1. Reading

Introduction to JavaScript, Part 2

Outline. Lecture 4: Document Object Model (DOM) What is DOM Traversal and Modification Events and Event Handling

JSF - H:SELECTONEMENU

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

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

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2018)

IS 242 Web Application Development I

DC71 INTERNET APPLICATIONS JUNE 2013

Web Programming/Scripting: JavaScript

JavaScript. The Basics

Chapter 14 - Dynamic HTML: Event Model

JSF - H:SELECTONERADIO

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

JavaScript Introduction

write vs. writeln Prompting as Page Loads Today s Goals CSCI 2910 Client/Server-Side Programming Intermediate File vs. HTML Output

What Is JavaScript? A scripting language based on an object-orientated programming philosophy.

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

Phụ lục A. Sơ đồ các đối tượng trong trình duyệt

JSF - H:PANELGRID. JSF Tag. Rendered Output. Tag Attributes. The h:panel tag renders an HTML "table" element. Attribute & Description.

TEXTAREA NN 2 IE 3 DOM 1

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

JAVASCRIPT. JavaScript is the programming language of HTML and the Web. JavaScript Java. JavaScript is interpreted by the browser.

Dynamic Web Pages - Integrating JavaScript into a SAS Web Application Caroline Bahler, ASG, Inc.

UNIT - III. Every element in a document tree refers to a Node object. Some nodes of the tree are

Client vs Server Scripting

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

Web Development & Design Foundations with HTML5

introjs.notebook March 02, 2014

isnan function returns true if the argument is not a number otherwise it is false.

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

So don t copy files or photocopy - Share! Share these FREE Courses! End User License Agreement Use of this package is governed by the following terms:

Exercise 1: Basic HTML and JavaScript

Digitizing Sound and Images III Storing Bits

Indian Institute of Technology Kharagpur. Javascript Part III. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. I.I.T.

CITS1231 Web Technologies. JavaScript Math, String, Array, Number, Debugging

Continues the Technical Activities Originated in the WAP Forum

c122mar413.notebook March 06, 2013

JavaScript Introduction

CISC 1600 Lecture 2.4 Introduction to JavaScript

334 JavaScript/JScript: Functions Chapter 11. boss. worker1 worker2 worker3. Hierarchical boss function/worker function relationship.

CITS1231 Web Technologies

CICS 515 a Internet Programming Week 3. Mike Feeley

This tutorial has been designed for beginners in HTML5 to make them understand the basicto-advanced

Skyway Builder Web Control Guide

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

Web Designing Course

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

5. Strict mode use strict ; 6. Statement without semicolon, with semicolon 7. Keywords 8. Variables var keyword and global scope variable 9.

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

Web GUI Development CSE5544

JAVASCRIPT BASICS. Type-Conversion in JavaScript. Type conversion or typecasting is one of the very important concept in

Lesson 7: If Statement and Comparison Operators

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 s role on the Web

Transcription:

LECTURE-2 Functions review HTML Forms Arrays Exceptions Events 1

JAVASCRIPT FUNCTIONS, REVIEW Syntax function <functionname> (params) { // code Note: Parameters do NOT have variable type. 1. Recall: Function definition can be in <head> part of HTML file. <body> portion of HTML file An external file. 2. return value of the function is optional. 2

FUNCTIONS EXAMPLE1 <html> <head> <title> Example of a simple function </title> <script type="text/javascript"> function factorial (input) { product = 1; for (i=1; i <= input; i++) product *= i; document.write ("factorial of i " + product); </script> </head> <body> <h1> Example of a simple function </h1> <script type="text/javascript"> factorial (9); </script> </body> </html> 3

FUNCTIONS EXAMPLE2 <html> <head> <title>browser Information example</title> <script type="text/javascript"> function BrowserInfoFn( ) { var browser = navigator.appname; var version = navigator.appversion; var ver = parsefloat (version); document.write ("Broswer: " + browser + " version: + version + " ver: " + ver + "<br />"); </script> </head> <body> <h1>browser Information example</h1> <script type="text/javascript"> BrowserInfoFn( ); </script> <hr> </body> </html> 4

SPECIAL FUNCTIONS IN JAVASCRIPT encodeuri encodes special characters of a URI, except:, /? : @ & = + $ # encodecomponenturi Encodes special characters and, /? : @ & = + $ # of a URI decodeuri Opposite of encodeuri decodecomponenturi Opposite of decodecomponenturi escape encodes special characters, except: * @ - _ +. / unescape Opposite of escape decodes a string eval Evaluates and executes a string as Javascript code isfinite Finds out if argument is a finite, valid number isnan Finds out if argument is not a number Number Converts a string to integer String Converts argument to string parsefloat Parses argument and returns a float value parseint Parses argument and returns an integer value 5

ARRAYS Arrays: Hold multiple objects E.g., array of strings, array of numbers, etc. E.g., var mycars = [ Toyota, Honda, BMW ]; or var mycars = new Array( ); mycars[0] = Toyota"; mycars[1] = Honda"; mycars[2] = "BMW"; or var mycars=new Array( Toyota", Honda","BMW"); mycars.push( Acura, Lexus ); // Add more cars document.write (mycars); // Toyota, Honea, BMW, Acura, Lexus mycars.pop( ); // Get the last car here Lexus 6

ARRAYS Useful array functions push Add an element at the end pop Remove the last element added length Get the number of elements added tostring Convert to a string. Elements are, separated shift Removes and returns first element Unshift adds an element at the beginning Ref: https://www.w3schools.com/js/js_array_methods.asp 7

VALUES OF JS POPUP BOXES Confirm box var i = confirm ( Press OK or Cancel ) Prompt box var i = prompt ( Enter some value, default ); Alert box alert ( alert text ); 8

HTML FORMS We covered some HTML tags earlier. HTML form Another HTML tag Useful to send information from browser to server Can use other HTML tags <input> <button> <submit> <select> and <option> <textarea> Javascript functions can be used to verify HTML forms input 9

HTML FORM EXAMPLE <input id="id1" type="number" min="100" max="300" required> <button onclick="myfunction()">ok</button> <p id="demo"></p> <script> function myfunction() { var inpobj = document.getelementbyid("id1"); if (inpobj.checkvalidity() == false) { document.getelementbyid("demo").innerhtml = inpobj.validationmessage; </script> 10

EXCEPTIONS Syntax and usage Similar to Java/C++ exception handling try { // your code here catch (excptn) { // handle error // optional throw 11

EXCEPTIONS EXAMPLE <html> <head> <title> Error handling example </title> <script type="text/javascript"> function handleerror( ) { try { i = foo( ); // Calling an undefined function raises an exception return i; catch (err) { alert ("Something was wrong"); return ("Error!"); </script> </head> <body> <script type="text/javascript"> var i = handleerror(); document.write ("i: " + i); </script> </body> </html> 12

THROW Syntax throw (exception) Example try { // some code catch (error) { // handle error throw (error2) 13

THROW EXAMPLE <html> <body> <script type="text/javascript"> var x = prompt ("Enter a number between 0 and 10:",""); try { if (x > 10) throw "Err1"; else if (x < 0) throw "Err2"; catch (er) { if (er == "Err1") </script> </body> </html> alert ("Error! The value is too high"); if (er == "Err2") alert ("Error! The value is too low"); 14

HANDLING ERRORS ONERROR onerror an event to handle errors. onerror = handleerr // Call handleerr on errors function handleerr(msg,url,l) // msg error msg, url current URL, l line # { //Handle the error here return true or false 15

EVENTS Events in Javascript something happening. Examples Web page is loaded/unloaded Mouse key clicked/double-clicked Mouse hovering over/out-of a region Any keyboard key is pressed/released An error has occurred A submit or reset button is pressed. An element gets or loses focus. Complete list of events are given at http://www.w3schools.com/jsref/jsref_events.asp 16

EVENTS ONLOAD( ) Called (if defined) when a web page is loaded. Simple Example <html> <head> <title> On Load event example </title> <script type="text/javascript"> function onloadfn( ) // Function definition { alert ("Web page finished loading"); </script> </head> <body onload="onloadfn( )">// Call it when page is loaded </body> </html> 17

EVENTS ONUNLOAD Opposite of onload Called when We go out of a web page or A web page is re-loaded. Example Same example as before Except replace onload with onunload. 18

EVENTS ONERROR Recall our example of error handling Example: onerror=handleerr // Call handleerr on errors function handleerr(msg,url,l) // msg error msg, url current URL, l line # { //Handle the error here return true or false 19

EVENTS ONSUBMIT, ONRESET onsubmit Event when a submit button is pressed. E.g., When using forms. onreset event when a reset button is pressed. Typically, used to cancel/reset the values of all fields. 20

EVENTS ONMOUSEUP, ONMOUSEDOWN, ONMOUSEOVER, ONMOUSEOUT onmousedown event when a mouse button is pressed down. onmouseup event when a mouse button is pressed up. onmouseover event when a mouse hovers over (a specific region). onmouseout event when a mouse comes out (of a specific region). 21

EVENTS ONKEYPRESS, ONKEYDOWN, ONKEYUP onkeypress Event when a key is pressed onkeydown Event when a key is pressed or held down Similar to onkeypress onkeyup Event when a key is released (after being pressed) 22

EVENTS ONCLICK, ONDBLCLICK, ONCHANGE onclick event when a button is clicked ondblclick event when a button is double clicked Try to avoid onclick when ondblclick is defined 23

EVENTS ONFOCUS, ONBLUR, ONRESIZE onfocus event when an element gets focus onblur event when an element loses focus Opposite of onfocus 24

EVENTS ONRESIZE, ONCHANGE, ONABORT onresize event when a browser window is resized (changed). onchange event when the value of a field changes onabort event when loading of an image is interrupted. 25

TIMING EVENTS settimeout: execute something after a given time Syntax: var t = settimeout (code, time_in_msec); Similar to sleep Difference with sleep: Code settimeout is executed immediately, no timeout there. cleartimeout: Cancel a timeout condition Syntax: cleartimeout (t) t was the variable returned by settimeout 26