INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.

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 COMM 1213 H1 COMP 4923 X1 JavaScript 1 (Readings: Ch. 10, 11 Knuckles)
Javascript Introduction Norman White Material is from w3schools.com Go there to run examples interactively.
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
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.
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.
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
آموزش طراحی وب سایت جلسه دهم – جاوا اسکریپت 1 تدریس طراحی وب برای اطلاعات بیشتر تماس بگیرید تاو شماره تماس: پست الکترونیک :
JavaScript CMPT 281. Outline Introduction to JavaScript Resources What is JavaScript? JavaScript in web pages.
JavaScript: Control Structures September 27, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel,
JavaScript JavaScript is a scripting language that is most commonly used to add client- side programming to a web page. Some of the things it is used for.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
1 CLIENT-SIDE SCRIPTS. Objectives 2 Learn how to reference objects in HTML documents using the HTML DOM and dot syntax Learn how to create client-side.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
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)
SE-2840 Dr. Mark L. Hornick 1 Modifying webpage behavior using client-side scripting Javascript.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
1 JavaScript in Context. Server-Side Programming.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
Using Client-Side Scripts to Enhance Web Applications 1.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
JavaScript Introduction 1. What is Java Script ? JavaScript is a client-side scripting language. A scripting language is a lightweight programming language.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
LOGO Introduction to Client-Side Scripting and JavaScript CHAPTER 9 Eastern Mediterranean University School of Computing and Technology Department of Information.
1 JavaScript
JavaScript - Basic Concepts Prepared and Presented by Hienvinh Nguyen, Afshin Tiraie.
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.
05 – Java Script (1) Informatics Department Parahyangan Catholic University.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 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.
JavaScript, Fourth Edition
COMP403 Web Design JAVA SCRİPTS Tolgay KARANFİLLER.
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.
1 JavaScript in Context. Server-Side Programming.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Pertemuan 5 IT133 Pengembangan Web JavaScript. What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
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,
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
REEM ALMOTIRI Information Technology Department Majmaah University.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Javascript Prof. Wenwen Li School of Geographical Sciences and Urban Planning 5644 Coor Hall
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
PHP using MySQL Database for Web Development (part II)
JavaScript Syntax and Semantics
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
JavaScript.
JavaScript an introduction.
WEB PROGRAMMING JavaScript.
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
CS105 Introduction to Computer Concepts
JavaScript CS 4640 Programming Languages for Web Applications
Tutorial 10: Programming with javascript
Web Programming– UFCFB Lecture 13
CS105 Introduction to Computer Concepts JavaScript
JavaScript CS 4640 Programming Languages for Web Applications
Presentation transcript:

INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE

22 Outline Overview of JavaScript Scratchpad Javascript Basics Three programming Constructs Built-in functions Next Class – More about JavaScript functions

Overview of JavaScript Originally developed in Dec 1995 by Brendan Eich at Netscape.Brendan Eich Not Java Standard version is called ECMAScript, The ECMAScript standard is documented in the ECMA-262 specification JavaScript without purchasing a license. 3

Overview of JavaScript small, light-weight, cross-platform, object-oriented scripting language. Interpreted language Loose data-type programming language adding interactivity to static HTML pages. embedded to HTML pages working in major browsers, e.g, I.E. Firefox, Chrome, Opera, Safari. Everyone can use 4

What Can JavaScript do? usually on client side to add interactivity to the HTML documents. also possible to use JavaScript for server-side programming. also significantly used for other applications outside of web pages 5

What Can JavaScript do? Respond to events on the web pages, such as button clicks, mouse moving, objects getting/ losing focus. Validate user’s input before sending out the request to server. Create/open or close a window in run time. Write information to web pages dynamically. Change web page contents. Change web page styles. Build small but complete client-side applications. Communicate asynchronously with the server. Develop visualizations, animations, and games on web pages without the need for a Java applet or Flash plugin. 6

Demo ChangeContent.html ChangeStyle.html Dynamic_window.html Pizza.html Title_game.html 7

Scratchpad Firefox -> Tools -> Web Developer -> Scratchpad Mozilla Developer Network (MDN) online help documentation: US/docs/Tools/Scratchpad US/docs/Tools/Scratchpad

Scratchpad Input: var x = prompt(“Enter a number”, “0”); // x is a string Output: alert(“information to display”);

Example var x = 2; x = parseInt(prompt("Input a number", "")); var y =0; y = parseInt(prompt("Input 2nd number"," ")); var z = x+y; alert (z); // alert(“x+y=” +z); //alert(x+ "+" + y + "=" + z);

JavaScript MDN (Mozilla Developer Network) Javascript Guide: JavaScript is a loosely typed language. Do not have to specify the data type of a variable when you declare it. Data types are converted automatically as needed during script execution. 11

12 Data Types Primitive types – Boolean, Number, String Complex type – Object (including Array, Function, Date, Math, and RegExp) Special values – Null, Undefined var variableName; var variableName = "This is a String"; var variableName = 'This is a String'; var variableName = 45.55; var variableName = 45; var variableName = true; var variableName = null; var myDate = new Date(); var x = prompt(“Enter a number”, “0”);

13 JavaScript Comments Comments can be added to explain the JavaScript, or to make the code more readable. Single line comments start with //. // this is a single line comment Multi line comments start with /* and end with */. /* these are multi-line comments */ Using Comments to Prevent Execution // document.write(" This is a heading "); /*document.write(" This is a paragraph. "); document.write(" This is another paragraph. ") */ Using Comments at the End of a Line document.write (“hello world.”); // write “hello world”

14 JavaScript Variables A JavaScript variable naming rules: - must start with a letter or underscore ("_") -subsequent characters can be letters, underscores or digits. Declare (Create) a variable” -var courseName; -If do not initialize a variable, its value is “undefined” Assign values to variables: courseName=“BTI220”; Initialization: var courseName=“BTI220”;

15 JavaScript Variables Assigning Values to Undeclared JavaScript Variables the variables will automatically be declared and becomes Global variable. x = 5; Attention: x=5; x=“abc”; // allowed

16 Expressions An expression is any valid set of literals, variables, operators, and expressions that evaluates to a single value. The value may be a number, a string, or a logical value. Two types of expressions: 1) those that assign a value to a variable, e.g. x = 7. 2) those that simply have a value, e.g., simply evaluates to 7; it does not perform an assignment. JavaScript has the following kinds of expressions: 1)Arithmetic - evaluates to a number 2)String - evaluates to a character string 3)Logical - evaluates to true or false

17 A Conditional Expression Syntax: (condition) ? val1 : val2; If the condition is true, the expression has the value of val1, Otherwise it has the value of val2. You can use a conditional expression anywhere you would use a standard expression. e.g., grade = (mark >= 50) ? “pass" : “fail";

18 Arithmetic Operators OperatorDescriptionExampleResult +Additionx=y+2x=7 -Subtractionx=y-2x=3 *Multiplicationx=y*2x=10 /Divisionx=y/2x=2.5 %Modulus (division remainder) x=y%2x=1 ++Incrementx=++yx=6 --Decrementx=--yx=4 Given Y=5

19 Assignment operators Assign values to variables. Given x=10, y=5 OperatorExampleSame AsResult =x=y x=5 +=x+=yx=x+yx=15 -=x-=yx=x-yx=5 *=x*=yx=x*yx=50 /=x/=yx=x/yx=2 %=x%=yx=x%yx=0

20 The + Operator Used on Strings Concatenate two strings x=“abc” y=“123” x+y =>“abc123” x+” “+y => “abc 123”

21 Adding Strings and Numbers 1.x =5+5; document.write(x); 2.x="5"+"5"; document.write(x); 3.x=5+"5"; document.write(x); 4.x="5"+5; document.write(x); ,3,4. 55

Comparison Operators Given x=5: Operat or DescriptionExample ==is equal tox==8 is false x=8 is true ( in condition) x==“5” is true ===is exactly equal to (value and type) x===5 is true x==="5" is false !=is not equalx!=8 is true x!=5 is false !==is not equal (neither value nor type) x!==“5” is true x!==5 is false >is greater thanx>8 is false <is less thanx<8 is true >=is greater than or equal tox>=8 is false <=is less than or equal tox<=8 is true

23 if (y===“5") document.write("true"); else document.write("false"); y=5; if (y==“5") document.write("true"); else document.write("false"); if (y="7") document.write("true"); else document.write("false");  true  false  true if (y=== 5) document.write("true"); else document.write("false");  true

Summary of the Differences From C 1.Define a variable – var x=6; – x=“abc”; // allowed, even x was previously defined as a number 2.+ on strings – 5+”abc” – 5+”6” 3.Comparison with ==, === – 5==“5” //true – 5===“5” //false 4./ on integer numbers – var x=9; – var y=x/2; //4.5 5.% on double numbers – var x = 4.1 %2 ; //

Summary of the Differences From C (cont’d) Strings 1.assignment var x=“abc”; x =“def”; 2.Concatination 5+”abc” 5+”6” 3.Comparison var x=“abc”; var y=“abd”; if ( x<y) //true 4.String length var x=“abc”; var y=x.length; //3

Programming Constructs – Sequence var x = 5; var y=6; var z = x+y; alert(z);

27 Programming Constructs – Selection if if (mark > 90) grade=‘A+’; if/else if (mark > 90) grade=‘A+’; else grade=‘A’;

28 Switch statement: select one of many blocks of code to be executed. switch(n) { case 1: execute code block 1 break; case 2: execute code block 2 break; default: code to be executed if n is different from case 1 and 2 } Programming Constructs – Selection

29 y=1; switch (y) { case 1: alert("111"); case 2: alert("222"); case 3: alert("333"); default: alert("ddd"); } y=1; switch (y) { case 1: alert("111"); break; case 2: alert("222"); break; case 3: alert("333"); break; default: alert("ddd"); } ddd

30 Programming Constructs – Iteration while do/while for

31 The while loop while (condition) { action statement(s) } var i=1; while (i<5) { alert("week "+i + ":" + i); i++; }

32 The do...while Loop do { action statements } while (condition) var i=10; while (i<5) { alert(“week “+i); i++; }

33 The for loop for (initialExp ; condition ; updateExp) { statement(s) to be repeated } for (i=1; i<5; i++) { alert("week "+i ); }

34 Break Statements Break the loop and continue executing the code that follows after the loop (if any). var i=1; while (i<5) { alert(“week “+i); if (i==3) break; else i++; }

35 Break Statements Break the current loop and continue with the next value. (nested loops). var i=1; var j=1; while (i<5) { alert('week: '+i ); for (j=1; j<=7; j++) { alert('day:'+j +'of week:'+ i); if (j==3) break; } // for i++; } // while

36 Functions (1) built-in functions/ global functions (2) user-defined functions/ custom built functions

Where to use JavaScript Functions Similar to C functions, defined/called in applications for event handlers on the web pages. Be triggered/ executed/ called/invoked when some events occur on the web page – JavaScript functions are actions or behaviors that are associated with the events on web pages. associated to an object to specify the behavior of the object. – a method or a member function. 37

38 Built-in functions Are functions that are built into the JavaScript language. They have already been defined and the logic behind them has already been coded for your use - you simply use them.

39 Built-in functions eval()  one argument: a string.  If the string is an expression, eval() evaluates the expression.  If the string is made up of JavaScript statements, eval() performs the statements. Syntax : eval(string) var x = 2; var y = 4; alert(x+y); alert(eval("x+y")); // 6

40 Built-in functions isNaN()  to determine if an argument is "NaN" (not a number). Syntax : isNaN(a test value) alert(isNaN("avc")); // true alert(isNaN("123")); //false alert(isNaN(123)); // false

41 Built-in functions parseFloat():  parses a string and returns a floating point number.  If a character other than a numeral, a sign (+ or -), or an exponent is found, the function returns the value up to that point.  If the first character in the string cannot be converted to a number, the function returns "NaN". Syntax : parseFloat(string ) alert(parseFloat("12.66")); // alert(parseFloat("12.4ff")); // 12.4 alert(parseFloat("abc")); // NaN

42 Built-in functions parseInt()  parses its first argument (a string), and then tries to return an integer of the specified radix (or base).  If a number in the string is beyond the base, parseInt() ignores the rest of the characters and returns an integer value up to that point.  If the first character can't be converted to a number, parseInt() returns "NaN". Syntax : parseInt("string", base) parseInt(15.99, 10) // 15 parseInt("F", 16) // 15 parseInt(“10”, 8) // 8

Built-in functions Number()/ String() convert an object to a number or a string. x = "12.78"; y = 10; z = Number(x) + y; alert("sss =" + String(y)); 43

Built-in functions toFixed() formats a number to a specific number of digits to the right of the decimal. var x = ; alert(x.toFixed()); // 13 note that, 0 digit alert(x.toFixed(2) ); // note the rounding alert(x.toFixed(6)); // Note: this is a function of Number object instead of a global function

Summary of Built-in Functions prompt() alert() eval() isNaN() parseInt() parseFloat() Number() String() toFixed()

46 User-defined functions Syntax: function fname(var1,var2,...) { code block } Need to call to execute fname(arg1, arg2,…); Do not need to specify return data type

47 User-defined functions Function with return values The return statement is used to specify the value that is returned from the function. function fname(var1,var2,...) { code block return value; } Eg, function sq(x) { return x*x; } y = 10; alert(sq(y));

User-defined functions If the function does not have a return statement it still has a value undefined (except the constructor). if (funcName(arg1, arg2,...) === undefined) // true { // will do something here... } 48

Functions The scope of a variable: the scope available to access. Local variables – Declared (using var) within a JavaScript function becomes LOCAL – can only be accessed from within that function. – the variable has local scope. Can have local variables with the same name in different functions, – because local variables are only recognized by the function in which they are declared. Local variables are deleted as soon as the function is completed. Global variables – Declared outside a function – all scripts and functions on the web page can access it.

Functions Lifetime of JavaScript variables – starts when they are declared. – Local variables are deleted when the function is completed. – Global variables are deleted when you close the page. Undeclared JavaScript Variables – are GLOBAL variables.

Example function f() { x = "abc "; // global variable alert("x is global: "+ x); // ok, display “abc” var y= "abd"; // local variable alert(“y is local: " +y); // ok, display “abd” } f(); alert("outside function, x = "+ x); // output: abc***: alert("outside function, y = "+ y); // Exception: y is not defined

52 Guidelines JavaScript is Case Sensitive courseName != coursename Semicolon is optional White Space – JavaScript ignores extra spaces. They are equivalent: courseName=“INT222"; courseName = “INT222"; – sometimes, it is different with or with no space: var x=6; var s1 = “ab c”; var s2= “ab c”;

Next Class More about JavaScript functions Closure Anonymous functions Recursion 53

Thank you!