More JavaScript. Browser support JavaScript works on the following browsers: –Netscape 2 and up –Opera 3 and up –Hotjava 3 and up –iCab on Mac Classic.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Introducing JavaScript
1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
The Web Warrior Guide to Web Design Technologies
JavaScript Language Fundamentals. About JavaScript JavaScript is not Java, or even related to Java –The original name for JavaScript was “LiveScript”
12-Jun-15 JavaScript Language Fundamentals I. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 4 Client-Side Programming:
Tutorial 10 Programming with JavaScript
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
Introduction to Web Site Development Steven Emory Department of Computer Science California State University, Los Angeles Lecture 8: JavaScript I.
25-Jun-15 JavaScript Language Fundamentals II. 2 Exception handling, I Exception handling in JavaScript is almost the same as in Java throw expression.
28-Jun-15 More JavaScript. 2 Browser support JavaScript works on almost all browsers Internet Explorer uses JScript (referred to in menus as “Active Scripting”),
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
JavaScript, Third Edition
XP Tutorial 1 New Perspectives on JavaScript, Comprehensive1 Introducing JavaScript Hiding Addresses from Spammers.
Introduction to scripting
JavaScript: Control Structures September 27, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel,
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
Programming with JavaScript (Chapter 10). XP Various things Midterm grades: Friday Winter Career Fair – Thursday, April 28, 2011 (11 am to 3 pm). – MAC.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Data Types.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
Chapter 3 : Processing on the Front End JavaScript Technically its name is ECMA-262, which refers to the international standard which defines it. The standard.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Chapter 3: Data Types and Operators JavaScript - Introductory.
SE-2840 Dr. Mark L. Hornick 1 Modifying webpage behavior using client-side scripting Javascript.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
1 JavaScript in Context. Server-Side Programming.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
Tutorial 10 Programming with JavaScript. XP Objectives Learn the history of JavaScript Create a script element Understand basic JavaScript syntax Write.
Tutorial 10 Programming with JavaScript
Done by: Hanadi Muhsen1 Tutorial 1.  Learn the history of JavaScript  Create a script element  Write text to a Web page with JavaScript  Understand.
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
4.4 JavaScript (JS) Deitel Ch. 7, 8, 9, JavaScript & Java: Similarities JS (JavaScript) is case-sensitive Operators –arithmetic: unary +, unary.
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
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.
JavaScript, Fourth Edition
JavaScript History and Versions JavaScript was introduced as part of the Netscape 2.0 browser Microsoft soon released its own version called JScript ECMA.
1 JavaScript in Context. Server-Side Programming.
1 Lecture 9 Client-Side Programming: the JavaScript Language.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
Javascript Intro Instructor: Shalen Malabon. Lesson Plan Core Javascript Syntax Types and Objects Functions DOM Event Handling Debugging Javascript Smarter.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
JavaScript and AJAX 2nd Edition Tutorial 1 Programming with JavaScript.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
Basic Objects. Math Object  Math.cos( x ), x in radians  Math.sqrt ( x )  Math.pow ( x, y )  Math.ceil( x )  etc.
Chapter 6 JavaScript: Introduction to Scripting
Unit 2 Client-Side Programming: the JavaScript Language
Tutorial 10 Programming with JavaScript
JavaScript Objects.
Scope, Objects, Strings, Numbers
More JavaScript 12-Nov-18.
JavaScript and Ajax (Expression and Operators)
JavaScript an introduction.
Web Systems Development (CSC-215)
Chapter 4 Client-Side Programming: the JavaScript Language
PHP.
Tutorial 10 Programming with JavaScript
CS3220 Web and Internet Programming JavaScript Basics
Introducing JavaScript
PHP an introduction.
More JavaScript 4-May-19.
CS3220 Web and Internet Programming JavaScript Basics
Presentation transcript:

More JavaScript

Browser support JavaScript works on the following browsers: –Netscape 2 and up –Opera 3 and up –Hotjava 3 and up –iCab on Mac Classic –Omniweb and WebTV on Mac OS X –QNX Voyager and Konquerer on Linux KDE Internet Explorer uses JScript, which is Microsoft’s version of JavaScript Older browsers don’t support some of the newer features of JavaScript –We will assume modern browser support

What you can’t do To protect the visitor to your web pages, you can’t: –Read or write user files However, JScript on IE allows ASP scripting, which is how the very destructive worm spreads –Execute any other programs –Connect to any other computer, except to download another HTML page or to send –Determine what other sites the user has visited –Open a very small (less than 100px by 100px) window or an offscreen window (except in IE)

Debugging Mozilla/Netscape has much better debugging tools than IE –Mozilla Select Tools => Web Development => JavaScript console –Netscape 6: Select Tasks => Tools => JavaScript console –Netscape 4: Select Communicator => Tools => JavaScript console –Any Mozilla or Netscape: Type javascript: in the location bar and press Enter –Internet Explorer: Go to the Preferences... dialog and look for something like Web content => Show scripting error alerts After debugging, test your program in IE –IE is the most popular browser

Numbers In JavaScript, all numbers are floating point Special predefined numbers: –Infinity, Number.POSITIVE_INFINITY -- the result of dividing a positive number by zero –Number.NEGATIVE_INFINITY -- the result of dividing a negative number by zero –NaN, Number.NaN (Not a Number) -- the result of dividing 0/0 NaN is unequal to everything, even itself There is a global isNaN() function –Number.MAX_VALUE -- the largest representable number –Number.MIN_VALUE -- the smallest (closest to zero) representable number

Strings and characters In JavaScript, string is a primitive type Strings are surrounded by either single quotes or double quotes There is no “character” type Special characters are: \0 NUL \b backspace \f form feed \n newline \r carriage return \t horizontal tab \v vertical tab \' single quote \" double quote \\ backslash \xDD Unicode hex DD \xDDDD Unicode hex DDDD

Some string methods charAt(n) –Returns the n th character of a string concat(string1,..., stringN) –Concatenates the string arguments to the recipient string indexOf(substring) –Returns the position of the first character of substring in the recipient string, or -1 if not found indexOf(substring, start) –Returns the position of the first character of substring in the given string that begins at or after position start, or -1 if not found lastIndexOf(substring), lastIndexOf(substring, start) –Like indexOf, but searching starts from the end of the recipient string

More string methods match(regexp) –Returns an array containing the results, or null if no match is found –On a successful match: If g (global) is set, the array contains the matched substrings If g is not set: –Array location 0 contains the matched text –Locations 1... contain text matched by parenthesized groups –The array index property gives the first matched position replace(regexp, replacement) –Returns a new string that has the matched substring replaced with the replacement search(regexp) –Returns the position of the first matched substring in the given string, or -1 if not found.

boolean The boolean values are true and false When converted to a boolean, the following values are also false : –0–0 –"0" and '0' – the empty string, '' or "" –undefined –null –NaN

undefined and null There are special values undefined and null undefined is the only value of its “type” –This is the value of a variable that has been declared but not defined, or an object property that does not exist –void is an operator that, applied to any value, returns the value undefined null is an “object” with no properties null and undefined are == but not ===

Arrays As in C and Java, there are no “true” multidimensional arrays –However, an array can contain arrays –The syntax for array reference is as in C and Java Example: var a = [ ["red", 255], ["green", 128] ]; var b = a[1][0]; // b is now "green" var c = a[1]; // c is now ["green", 128] var d = c[1]; // d is now 128

Determining types The unary operator typeof returns one of the following strings: "number", "string", "boolean", "object", "undefined", and "function" –typeof null is "object" –If myArray is an array, typeof myArray is "object" To distinguish between different types of objects, –myObject instanceof Constructor The “ Constructor ” should be an object that is a constructor function It is an error if the rhs is not an object at all –myObject.constructor == Constructor –myObject.toString() == "ConstructorName"

Wrappers and conversions JavaScript has “wrapper” objects for when a primitive value must be treated as an object –var s = new String("Hello");// s is now a String –var n = new Number(5);// n is now a Number –var b = new Boolean(true);// b is now a Boolean –Because JavaScript does automatic conversions as needed, wrapper objects are hardly ever needed JavaScript has no “casts,” but conversions can be forced –var s = x + "";// s is now a string –var n = x + 0;// n is now a number –var b = !!x;// b is now a boolean –Because JavaScript does automatic conversions as needed, explicit conversions are hardly ever needed

Variables Every variable is a property of an object When JavaScript starts, it creates a global object In client-side JavaScript, the window is the global object –It can be referred to as window or as this –The “built-in” variables and methods are defined here There can be more than one “global” object –For example, one frame can refer to another frame with code such as parent.frames[1] Local variables in a function are properties of a special call object

HTML names in JavaScript In HTML the window is the global object –It is assumed that all variables are properties of this object, or of some object decended from this object –The most important window property is document HTML form elements can be referred to by document.forms[formNumber].elements[elementNumber] Every HTML form element has a name attribute –The name can be used in place of the array reference –Hence, if Then instead of document.forms[0].elements[0] you can say document.myForm.myButton

More about with with ( object ) statement ; uses the object as the default prefix for variables in the statement As noted in an earlier lecture, one book hints at mysterious problems resulting from the use of with, and recommends against ever using it It turns out that there are two problems: –with is difficult to optimize, hence may be inefficient –More importantly, variable declarations and function definitions have odd and counterintuitive behavior The problem appears to be determining if the prefix is used Other types of statements are fine

Functions In Java, methods are associated with objects In JavaScript, a function is an object Functions can be recursive: –function factorial(n) { if (n <= 1) return 1; else return n * factorial(n - 1); } Functions can be nested: –function hypotenuse(a, b) { function square(x) { return x * x; } return Math.sqrt(square(a) + square(b)); }

The Function() constructor Since functions are objects, they have a constructor: –Function(arg1, arg2,..., argN, body) –All the arguments to the constructor are strings –Example: var f = new Function("x", "y", "return x * y;"); Notice that the function has no name –But you can assign it to a variable and use that name –The name can be used to call the function as usual You can construct functions dynamically in JavaScript (they are automatically compiled) –However, compilation is computationally expensive Functions defined in this way are always global

Function literals As we just saw, a function can be defined by means of a constructor: – var f = new Function("x", "y", "return x * y;"); A function can be written literally, as in the following example: –var f = function(x, y) { return x * y; } –This function is not necessarily global To write a recursive literal function, give it a name: –var f = function fact(n) { if (n <= 1) return n; else return n * fact(n - 1) ; }; –The name does not persist after the function is created

Function names The “name” of a function is just the variable that holds the function –var square = function(x) { return x * x; }; –var a = square(4);// a now holds 16 –var b = square;// b now holds square –var c = b(5);// c now holds 25 –var d = [ b ];// d is an array –var e = d[0](6);// e now holds 36

The call object When a function is called, a new call object is created –The properties of the call object include: The function parameters Local variables declared with the var statement The arguments object

arguments The arguments object is like an array –arguments[n] is a synonym for the n th argument –arguments.length is the number of arguments that the function was called with function.length is the number of arguments it was defined with arguments.length, unlike function.length, is available only within the function –arguments.callee is the function itself

Example uses of arguments function max() { var m = Number.NEGATIVE_INFINITY; for (var i = 0; i m) m = arguments[i]; } return m; } function(n) { if (n <= 1) return 1; return n * arguments.callee(n - 1); }

Methods When a function is a property of an object, we call it a “method” –A method can be invoked by either of call(object, arg1,..., argN) or apply(object, [arg1,..., argN]) – call and apply are defined for all functions call takes any number of arguments apply takes an array of arguments –Both allow you to invoke a function as if it were a method of some other object, object –Inside the function, the keyword this refers to the object

Properties of functions Since a function is an object, you can add properties to it –Function properties are often a good alternative to global variables –Example: uniqueInteger.counter = 0; function uniqueInteger() { return uniqueInteger.counter++; } –Function properties are a bit like static variables in Java

The End