1 Lecture #6 Dynamic Web Documents HAIT Summer 2005 Shimrit Tzur-David.

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

JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
Java Script Session1 INTRODUCTION.
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
EIW: Javascript the Language1 The JavaScript Language.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 Representation and Management of Data on the Web Client Side Programming.
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.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
Introduction to scripting
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Lectured By: Vivek Dimri Asst. Professor, SET, Sharda University
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
1 Representation and Management of Data on the Internet.
Netprog: JavaScript1 JavaScript Client-side dynamic documents.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
CISC474 - JavaScript 03/02/2011. Some Background… Great JavaScript Guides: –
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Javascript. Outline Introduction Fundamental of JavaScript Javascript events management DOM and Dynamic HTML (DHTML)
Chapter 3: Data Types and Operators JavaScript - Introductory.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet.
1 Representation and Management of Data on the Web Javascript.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
JS Basics 1 Lecture JavaScript - Basics. JS Basics 2 What is JavaScript JavaScript is a “simple”, interpreted, programming language with elementary object-
JavaScript Syntax, how to use it in a HTML document
JavaScript - Basic Concepts Prepared and Presented by Hienvinh Nguyen, Afshin Tiraie.
Overview of Form and Javascript fundamentals. Brief matching exercise 1. This is the software that allows a user to access and view HTML documents 2.
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.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
JavaScript, Fourth Edition
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Java Script About Java Script Document Object Model Incorporating JavaScript Adding JavaScript to HTML Embedding a Javascript External Scripts Javascript.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
JavaScript 101 Introduction to Programming. Topics What is programming? The common elements found in most programming languages Introduction to JavaScript.
1 Javascript CS , Spring What is Javascript ? Browser scripting language  Dynamic page creation  Interactive  Embedded into HTML pages.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Expressions and Data Types Professor Robin Burke.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
CGS 3066: Web Programming and Design Spring 2016 Introduction to JavaScript.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
JavaScript and Ajax Week 10 Web site:
REEM ALMOTIRI Information Technology Department Majmaah University.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Chapter 6 JavaScript: Introduction to Scripting
“Under the hood”: Angry Birds Maze
Introduction to Scripting
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
JavaScript.
JavaScript an introduction.
WEB PROGRAMMING JavaScript.
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
CS5220 Advanced Topics in Web Programming JavaScript Basics
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
JavaScript CS 4640 Programming Languages for Web Applications
Tutorial 10: Programming with javascript
JavaScript: Introduction to Scripting
CS3220 Web and Internet Programming JavaScript Basics
JavaScript CS 4640 Programming Languages for Web Applications
Presentation transcript:

1 Lecture #6 Dynamic Web Documents HAIT Summer 2005 Shimrit Tzur-David

2 Client-Server Architecture In a client-server architecture, computation is done either in the client or in the server There are cases where we can choose whether to perform the computation in the client or in the server There are cases where we cannot choose where to perform the computation –For example, accessing a database

3 Form Validation Consider the case where a user is required submit his name to a server application Where, for example, should we check that the inserted value is not an empty string? –In the client (i.e., the Web browser)? –In the server?

4 Client Side Technologies Javascript –Developed by Netscape –Supported by all browsers (although not all support the standard) –Very light (no graphics) and good interaction with HTML Java Applets –Developed by Sun –In the past it was supported by all browsers, but not any more –Capable of doing almost anything that Java allows

5 JavaScript Overview A "scripting" language for HTML pages The code is embed in HTML pages The browser interprets and executes the script (it is not compiled) Do not declare data types for variables (loose typing) Dynamic binding – object references checked at runtime

6 Overview – Cont. Scripts can manipulate "browser objects:" –HTML form elements –Images –Frames –etc. For security – cannot write to disk (when run on a client)

7 Abilities Generating HTML content dynamically Monitoring and responding to user events Validate forms before submission Interact with the frames and windows of the browser Customize pages to suit users

8 Example An Hello World Example document.write(" "); document.writeln("Hello World! "); document.writeln("What a boring example ") Why do we need if we used writeln?

9 Example – Cont.

10 Example 2 An Hello World Example document.write(" "); document.writeln("Hello World! "); document.writeln("What a boring example ") My Hello World Example

11 Example 2 – Cont.

12 Example 3 An Hello World Example document.write(" "); document.writeln("Hello World! "); My Hello World Example document.writeln("What a boring example")

13 Example 3 – Cont.

14 JavaScript Variables Untyped! Can be declared with var keyword: var foo; Can be created automatically by assigning a value: val = 1; val = “ Thank for all the fish"; val has changed from an int to a String!

15 Variables Names A variable name can be any valid identifier The identifier is a series of characters –Consisting of letters (lower and upper case), digits, and underscores (_) –Does not begin with a digit –Does not contain any space Javascript is case sensitive (foo and FOO are different)

16 Variables Local variable is available only in the function where it is declared Global variable is available all over the document A variable declaration –Inside a function creates a local variable –Outside a function creates a global variable Local variables must be declared with var

17 Literals The typical bunch: –Numbers –Strings“ Let it be ” –Boolean: true false –Arrays: [1, “ ab ba ”,17.234] –null –undefined Arrays can hold anything!

18 Operators Arithmetic, comparison, assignment, bit wise, Boolean (pretty much just like Java) + - * / % == != > = <= && || ! & | > += -= *= /= %=

19 The Special Plus Command Which of the following two is correct? What is the ‘type’ of the answer? x = “The answer is” + 42 y = 42 + “is the answer” String

20 Which is correct? Which of the following two is correct? What is the ‘type’ of the answer? x = "37" + 7 y = "37" - 7 String, output: 377 Number, output: 30

21 Types of Equality The equals == checks if both operands are equal after performing type conversion The equals === checks if both operands are of the same type and equal Example: –Is 3 == "3" (true or false?) –Is 3 === "3" (true or false?) true false

22 Conditional Operators equals if (a == b) {…} not equals if (a != b) {…} greater than or equal to if (a >= b) {...} less than or equal to if (a <= b) {...}

23 Boolean Operators and if (true && true) {…} or if (true || false) {…} not if (! false) {...}

24 Using Variables var firstNumber = 11, secondNumber = 23, sum; sum = firstNumber + secondNumber; document.write(" The sum of " + firstNumber + " and " + secondNumber + " is "); document.write(" " + sum + " ");

25

26 JavaScript Constructs condition (selection) if (condition) {statements if true} else {statements if false} if (metushelahAge < yourAverageAge) { document.write (" its true that Metushelah is younger than most of you,") document.write (" computers never lie! ") }

27 JavaScript Constructs loop (iteration): both for and while loops are available for (var i=0; i < myform.myAge.value.length; i++) { var oneChar=myform.myAge.value.substring (i, i+1) if (oneChar "9") { alert("Please enter a valid number " + oneChar + " is not valid.") }

28 Loops Example for (var counter = 1 ; counter <= 8 ; ++counter) { document.write( “ Now with font size " + counter + " “ ); }

29

30 JavaScript Functions Functions are first class citizens The keyword function used to define a function (subroutine): function add(x,y) { return(x+y); }

31 Function Input and Output Numbers and Boolean values always passed to functions using call-by-value For objects, a call-by-reference is used to pass them to the functions Numbers and Boolean values are always returned by value Objects returned by reference

32 Example The next example uses functions to computing the Fibonacci numbers Note the use of recursion Be careful, some browsers may not deal well with very big numbers in the input (i.e., too many recursive calls)

33 Functions Example function fibonacciValue() { var value = parseInt(document.fibonacciForm.number.value ); window.status = "Calculating Fibonacci number for " + value; document.fibonacciForm.result.value = fibonacci(value); window.status = "Done Calculating Fibonacci number"; } function fibonacci( n ) { if (n == 0 || n == 1) return n; else return fibonacci( n - 1 ) + fibonacci( n - 2 ); }

34 Enter a number <INPUT TYPE = "button" VALUE = "Calculate" ONCLICK = "fibonacciValue()" Fibonacci Value

35 Function Arguments Within a function, its arguments can be accessed with arguments[i]. The number of arguments is arguments.length A function can be created that takes any number of arguments

36 Example function myConcat(separator) { result="" // initialize //What does this return? list // iterate through arguments for (var i=1; i<arguments.length; i++) { result += arguments[i] + separator } return result } myConcat(“%","red","orange","blue") red%orange%blue

37 Objects Objects have attributes and methods There are pre-defined objects and user-defined object types Using objects has similarity to the syntax of C/Java: objectName.attributeName objectName.methodName() objectName.attributeName objectName.methodName()

38 Objects Are Like Arrays myCar.make = "Ford" myCar.model = "Mustang" myCar.year = 66; myCar[“make”] = "Ford" myCar[“model”] = "Mustang" myCar[“year”] = 66;

39 function show_props(obj, obj_name) { var result = "" for (var i in obj) result += obj_name + "." + i + " = " + obj[i] + "\n" return result } myCar.make = Ford myCar.model = Mustang myCar.year = 66 So, the function call show_props(myCar, "myCar") would return the following:

40 Creating a New Object There are two ways to create new objects: Object Initializer: –objectName={prop1:val1, …, propN:valN} Constructor Function: –define a constructor function –create the new object using new

41 Example function car(make, model, year) { this.make = make this.model = model this.year = year } theMazda = new car(“Mazda", “323", 1991) theHonda = {make:”Honda”, year:1992, color:"red",wheels:4, engine:{cylinders:4,size:2.2}}

42 Creating a Method A method of an object is a function that has been assigned to the object: object.methodName = functionName

43 Example function displayCar() { var result = "A Beautiful " + this.year + " " + this.make + " " + this.model; document.writeln(result) } function car(make, model, year) { this.make = make this.model = model this.year = year this.displayCar = displayCar }