Introduction to Web Site Development Steven Emory Department of Computer Science California State University, Los Angeles Lecture 8: JavaScript I.

Slides:



Advertisements
Similar presentations
Introducing JavaScript
Advertisements

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
CS 898N – Advanced World Wide Web Technologies Lecture 14: JavaScript Chin-Chih Chang
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.
Tutorial 10 Programming with JavaScript
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
CIS101 Introduction to Computing Week 12 Spring 2004.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
Introduction to scripting
JavaScript CMPT 281. Outline Introduction to JavaScript Resources What is JavaScript? JavaScript in web pages.
4.1 JavaScript Introduction
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
JavaScript – Part II Data Types and Operations George Mason University June 3, 2010.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
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)
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 JavaScript in Context. Server-Side Programming.
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.
Using Client-Side Scripts to Enhance Web Applications 1.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
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.
Scott Marino MSMIS Summer Session Web Site Design and Authoring Session 8 Scott Marino.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
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
JavaScript Introduction.  JavaScript is a scripting language  A scripting language is a lightweight programming language  A JavaScript can be inserted.
COMP403 Web Design JAVA SCRİPTS Tolgay KARANFİLLER.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
JavaScript 1 COE 201- Computer Proficiency. Introduction JavaScript scripting language ▫Originally created by Netscape ▫Facilitates disciplined approach.
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.
Introduction to Web Site Development John Hurley Department of Computer Science California State University, Los Angeles Lecture 7: JavaScript I.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Working with the Window Object JavaScript considers the browser window an object, which it calls the window object.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
REEM ALMOTIRI Information Technology Department Majmaah University.
JavaScript and AJAX 2nd Edition Tutorial 1 Programming with JavaScript.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Introduction to Web Site Development Department of Computer Science California State University, Los Angeles Lecture 9: JavaScript.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
CHAPTER 10 JAVA SCRIPT.
Chapter 6 JavaScript: Introduction to Scripting
Tutorial 10 Programming with JavaScript
Introduction to Scripting
CHAPTER 4 CLIENT SIDE SCRIPTING PART 2 OF 3
WEB PROGRAMMING JavaScript.
T. Jumana Abu Shmais – AOU - Riyadh
JavaScript What is JavaScript? What can JavaScript do?
CS105 Introduction to Computer Concepts
Tutorial 10 Programming with JavaScript
JavaScript What is JavaScript? What can JavaScript do?
JavaScript: Introduction to Scripting
Web Programming– UFCFB Lecture 13
CS105 Introduction to Computer Concepts JavaScript
Presentation transcript:

Introduction to Web Site Development Steven Emory Department of Computer Science California State University, Los Angeles Lecture 8: JavaScript I

JavaScript History 1995 – Invented by Netscape (now Mozilla) Became a standard called ECMA-262 JavaScript is now a dialect of ECMA-262 Current version: JavaScript Support Firefox 3.5: JavaScript Supported Internet Explorer 8: JScript 5.8 JScript is Microsoft’s dialect of the ECMA-262 standard. They cannot use JavaScript because JavaScript is trademarked by Sun Microsystems.

More About JavaScript Widely considered one of the worst programming languages ever invented JavaScript is a misnomer JavaScript has nothing to do with Java Java-Script and Java ~ Monkey-Wrench and Monkey Google the following: JavaScript “Worst programming language” “JavaScript Sucks” “I hate JavaScript”

More About JavaScript Unfortunately, JavaScript is the dominant client- side scripting language of the world wide web IE supports VBScript, other browsers don’t So like it or not, if you want cross browser support, you’re stuck with JavaScript

Internal JavaScript JavaScript can be written inside an HTML file using the SCRIPT element Start and end tags are required Example: // your JavaScript code goes here

Notes About Internal JavaScript The SCRIPT element can go inside the HEAD Script is run before BODY is loaded The SCRIPT element can go inside the BODY Script is run as BODY is being loaded

JavaScript Data Types There are three basic data types that we need to worry about, numbers and strings Numbers 1, 2, 3, 1.0, 2.0, 3.0, -1.0, -2.0 Strings " This is a string of characters. " Boolean true, false

JavaScript Variables In an HTML page, sometimes we need to maintain the state of something To do this we can using a named variable The name can be any alpha-numeric character Just don’t start it off with a number To declare a variable, use the var keyword The syntax is: var variable-name = initial-value;

Variables Example Example: var x = 3; var y = 1.0; var str = "This is a string."; var b1 = true; var b2 = false;

Comments Comments are ignored by the web browser There are two types of comments Single Line // This is a comment! Multiple Line /* This is a comment! */

Comments Example Example: // this is a comment /* this is a comment as well it spans multiple lines */

The Window Object Every browser has a window object that you can access through your JavaScript There are five important functions that we need to know about window: window.alert(message); window.open(URL); window.showModalDialog(URL); window.prompt(message, defaultValue); window.confirm(message);

The Window Object Notice that: The object name, window, comes first Next is the period, which is called the member access operator Next is the name of the function Next is the left parenthesis Next is zero or more comma-separated arguments Next is the right parenthesis And finally, there is the semicolon

The Window Object Example Example: window.alert("This is an alert!"); window.prompt("How many people are in CS120?", 26); window.confirm("Are you sure you want to drop CS120?"); window.open(" window.showModalDialog("

Alerts window.alert displays a simple dialog box window.alert(message); message is a JavaScript string that you would like to have shown in an alert box

Prompts window.prompt displays a simple dialog box with a text input box, that allows the user to enter text This function is used for user input. var value = window.prompt(message, default-value); message is a JavaScript string that you would like to have shown in an prompt box default-value is a JavaScript string that you would like to have shown in the input box value is the JavaScript string that was in the input box when the user closed the prompt dialog box by pressing OK; if the user pressed CANCEL, the keyword null is returned.

Confirmation window.confirm displays a simple dialog box with a message and two button, OK and CANCEL This function is used for user input. var value = window.confirm(message); message is a JavaScript string that you would like to have shown in the confirm box value is the JavaScript boolean value that is true if the user pressed the OK button, and false if the user pressed the CANCEL button

Popup Windows window.open displays a popup window Note: Whether or not a popup window opens in a tab or a new window is up to the browser. window.open(URL); URL is a JavaScript string that represents the URL that you would like to open up in a popup window

Modal Dialogs window.showModalDialog opens up a modal dialog A modal dialog is a popup window that prevents you from going back to the parent document; you must answer a modal dialog before proceeding window.showModalDialog(URL); URL is a JavaScript string that represents the URL that you would like to open up in a popup window

JavaScript Functions A function is a famous programming construct. When called, a function executes zero or more statements A statement is the smallest unit of execution in a programming language Functions can be reused We have been using functions in computer programming since the 1950’s It is called Procedural Programming

JavaScript Functions A function in JavaScript is like a function in math This is how we do it in math The function definition (math): f(x) = x 2 Evaluating (calling) a function (math) f(2) = 4 f(3) = 9 f(4) = 16

JavaScript Functions This is how we do it in JavaScript The function definition (JavaScript): function f(x) // x is the argument { return x*x; // multiply x times x } Evaluating (calling) a function (JavaScript) var x = f(2); // evaluates to 4 var y = f(3); // evaluates to 9 var z = f(4); // evaluates to 16 If you have a return statement in your function, you may call the function and assign the result to a variable as in the example above

JavaScript Functions Every JavaScript function declaration has the following syntax: The function definition (JavaScript): function name(zero-or-more-comma-separated-arguments) { statement-list }

JavaScript Functions JavaScript functions can have zero arguments function hey() { window.alert("Yo!"); return 0; } JavaScript functions do not have to return anything (they can just do one or more things) function hey() { window.alert("Yo!"); window.alert("Yo again!"); }

JavaScript Functions Anytime you have a JavaScript function that doesn’t return something (doesn’t have the return statement), don’t assign it to variable function hey1() { return 0; } function hey2() { } var x = hey1(); // OK, function hey1 returns 0 var y = hey2(); // NOT OK!!!!!!!!! NO RETURN VALUE!!!!!

Expressions An expression is sequence of numbers, variable names, values, function calls, and operators that computes a single value 3 + 4, is an expression that computes 7 ((7 – 2)*4), is an expression that computes 20 There are many different types of expressions Mathematical Boolean Logical

Mathematical Expressions Addition: x + y Subtraction: x – y Multiplication: x * y Division: x / y Remainder (Modulus): x % y

Boolean Expressions A Boolean expression is an expression that computes either true or false Less Than: x < y Greater Than: x > y Less Than or Equal: x <= y Greater Than or Equal: x >= y Equal: x == y Not Equal: x != y

Logical Expressions A logical expression computes a value based on a truth table Logical OR: x || y If x is true or y is true, then the result is true; otherwise false Logical AND: x && y If both x and y are true, then the result is true; otherwise false Logical NOT: !x If x is true, result is false; otherwise true

Statements A computer program executes statements A statement is the smallest unit of execution in a computer program A statement should always be terminated by a semicolon in JavaScript There are many types of statements assignment return if… else… Plus many more!!!

Assignment Statements We pretty much covered it already, it uses the assignment operator, = Each one of the four statements below is an assignment statement var x = 0; var y; y = 10; x = – 3;

if… else… Statements We use the if… else… statement in conjunction with Boolean and logical expressions to execute branches of statements The syntax is: if(boolean-or-logical-expression) single-statement; else single-statement; Example: if(x < 10) y = 3; else y = 4;

if… else… Statements If multiple statements are needed, use curly brackets to label a block (a set of statements). The syntax is: if(boolean-or-logical-expression) { statement1; statement2; statement3; } else { statement1; statement2; statement3; }