XP Tutorial 10 Section 1 1 Programming with JavaScript.

Slides:



Advertisements
Similar presentations
/ 251 Internet Applications Ahmed M. Zeki Sem – / Chapter 8.
Advertisements

JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Introducing JavaScript
The Web Warrior Guide to Web Design Technologies
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Tutorial 10 Programming with JavaScript
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
XP Tutorial 1 New Perspectives on JavaScript, Comprehensive1 Introducing JavaScript Hiding Addresses from Spammers.
CIS101 Introduction to Computing Week 09 Spring 2004.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
Introduction to scripting
Tutorial 14 Working with Forms and Regular Expressions.
CS346 - Javascript 1, 21 Module 1 Introduction to JavaScript CS346.
JavaScript, Fifth Edition Chapter 1 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 Environment.
CSC 330 E-Commerce Teacher Ahmed Mumtaz Mustehsan Ahmed Mumtaz Mustehsan GM-IT CIIT Islamabad GM-IT CIIT Islamabad CIIT Virtual Campus, CIIT COMSATS Institute.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
What is Java Script? An extension to HTML. An extension to HTML. Allows authors to incorporate some functionality in their web pages. (without using CGI.
Lesson13. JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server.
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 Session 2: What is JavaScript?
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.
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.
Introduction.  The scripting language most often used for client-side web development.  Influenced by many programming languages, easier for nonprogrammers.
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.
Introduction to JavaScript CS101 Introduction to Computing.
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.
Sahar Mosleh California State University San MarcosPage 1 JavaScript Basic.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
JavaScript. JavaScript is the programming language of HTML and the Web. Easy to learn One of the 3 languages of all developers MUST learn: 1. HTML to.
JavaScript Introduction.  JavaScript is a scripting language  A scripting language is a lightweight programming language  A JavaScript can be inserted.
Web111a_chapt08.ppt HTML: Section 8 JavaScript CGI Programs (Server Side programs) Common Gateway Interface Run on server Communicate with user across.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 Server versus Client-Side Programming Server-SideClient-Side.
1 JavaScript in Context. Server-Side Programming.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
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,
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
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.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Tutorial 10 Programming with JavaScript
Introduction to Scripting
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
JavaScript.
Objectives Insert a script element Write JavaScript comments
WEB PROGRAMMING JavaScript.
T. Jumana Abu Shmais – AOU - Riyadh
Tutorial 10 Programming with JavaScript
Tutorial 10: Programming with javascript
An Introduction to JavaScript
JavaScript: Introduction to Scripting
CIS 136 Building Mobile Apps
Presentation transcript:

XP Tutorial 10 Section 1 1 Programming with JavaScript

XP Sec 10.1 Introducing JavaScript History Element Basic JavaScript Syntax Write Text to a Web Page 2

XP Introducing JavaScript Server-side programs are placed on the server that hosts a Web site Advantage: Access to databases not on client’s computer Disadvantage: Subject to limitations placed on server by system administrators Client-side programming runs programs on each user’s computer Advantage: Not subject to server limitations Disadvantage: Not able to access online databases 3

XP Introducing JavaScript Server-Side Programming Client-Side Programming 4

XP The Development of JavaScript JavaScript is a subset of Java Differences between Java and JavaScript: Java is a compiled language JavaScript is an interpreted language An interpreted language can be run directly without compiling, therefore JavaScript code can be inserted directly into an HTML or XHTML document Not as powerful as Java, but easier to use 5

XP Comparing Java and JavaScript 6

XP The Development of JavaScript Jscript is a version of JavaScript supported by Internet Explorer The European Computer Manufacturers Association (ECMA) develops scripting standards The standard is called ECMAScript but browsers still generally call it JavaScript 7

XP Working with the Script Element A JavaScript program can either be placed directly in a Web page file or saved in an external text file Insert a client-side script in a Web page when using the script element script commands 8

XP Inserting JavaScript into a Web Page File Each statement—also known as a command—is a single line that indicates an action for the browser to take The semicolon notifies the browser that it has reached the end of the statement 9

XP Writing Output to the Web Page An object is any item—from the browser window itself to a document displayed in the browser to an element displayed within the document. The document displayed in the web page The browser window The browser itself The URL of the current web page A paragraph, a table, an image Etc.... Note from this point on you MUST remember, and adhere to, the rules of case sensitivity. Your programs will not work if you do not. A method is a process by which JavaScript manipulates or acts upon the properties of an object document.write(); 10

XP Writing Output to the Web Page To write text to a Web page, use the following JavaScript commands: document.write(“text”); You can use the document.write method to generate HTML code _pg-573-document-write.htm

XP JavaScript Syntax JavaScript is case sensitive (Personal note: the bane of my existance ) document.write(“Hi there”); will work Document.write(“Hi there”); will not work document.Write(“Hi there”); will not work etc. etc. etc. Ignores most occurrences of extra white space JavaScript Rule: Do not break a statement into several lines If you must break lines use the continuation character: \ document.write(" "); document.write(" " \ "); You cannot use this technique in the middle of a quote or field _pg-576-breaking-lines.htm

XP 13 Go to my Web site and download the files for Tutorial 3 Unzip the files into a work folder on your flash drive Go to pg 563in text book and start the in-chapter exercise. Stop when you get to the end of the section on page 577

XP Tutorial 10 Sections 2 and 3 14 Programming with JavaScript-2

XP Sec 10.2 Variables Data Types JavaScript function 15

XP Declaring a JavaScript Variable A variable is a named item in a program that stores information You can declare variables with any of the following JavaScript commands: var variable; var variable = value; variable = value; Rules for JavaScript variable names: First character must be a letter or an underscore Remaining characters – letters, numbers or underscores (no spaces) Can not use “reserved words” (e.g. function, var, return ) Case Sensitive 16 Creates a variable without assigning any value. Creates a variable and assigns a value.

XP JavaScript Variables and Data A JavaScript variable type is determined by its contents: Numeric variables String variables Boolean variables Boolean variables can only have two values: true or false Null variables If a variable is declared with one type of content and later a different type of content is assigned, the variable’s type changes. (“weakly-typed” language) 17 ( has no value) Note these are different

XP JavaScript Syntax The + symbol has three uses in JavaScript 1. Used in arithmetic operations to perform addition. var num1 = 5, num2 = 10, total; total = num1 + num2; 2. Used as a literal: document.write("The value of num1 + num2 is: ", total) 3. Used to concatenate several text strings into a single text string. var name="John Doe"; document.write("Hello there " + name + " how are you?"); (Note the space after the word there and before the word how) Another way to do this would be to use commas to separate the values document.write("Hello there", name, "how are you?"); _pg-581-concatenate-strings.htm

XP JavaScript Syntax If you need to combine a variable with a literal you need to concatenate. What if you have five image files: file1.jpg, file2.jpg, file3.jpg, file4.jpg, file5.jpg Now, let’s say you want to set the file name up as a variable. var myvar = "file1"; document.write(myvar + ".jpg"); Will generate: file1.jpg Now for a more complex example _pg-581-concatenate-variables-simple.htm 208_pg-581-concatenate-variables-complex.htm

XP JavaScript Syntax Beyond the scope of this text so briefly: Sometimes you may need to use the + sign to take two variables and create a new variable name: var name=image; var num = 01; var newvar = name + num; document.write("The newvar = ", newvar); _pg-581-concatenate-to-create-new-variables.htm

XP A JavaScript Function A function is a collection of commands that performs an action or returns a value Other languages might use the term: subroutine, or procedure A function name identifies a function Parameters are values used by the function { function commands } The function is executed only when called by another JavaScript command _pg-583-first-function.htm reserved word function function name parameters

XP JavaScript Function 22 Parameters – correspond to userName and emServer in the function definition.  call to the showEM() function reserved word function function name parameters 212_pg-583-function-with-parm.htm

XP JavaScript Variable Scope A variable declared within a function has “local” scope and can only be used inside that function. A variable declared not inside any function has “global” scope and can be used by any script in the web page. 23 Variable loop_count has global scope. It can be used anywhere on the document Variable my_toggle has local scope within the flipImages() function.. It can only be used in the flipImages() function 215_pg-589-scope.htm

XP JavaScript Returning values It is very common to invoke a script that needs to perform some kind of calculation and return a value. For instance, what if you wanted a function that would calculate the area of a rectangle. You might pass the function two parameters, a length and a width. The function would then calculate the area and return the answer using the return statement. function areaCalc(length, width) { var area = length * width; return area; } document.write("area of 4x5 rectangle: " + areaCalc(4,5)); _pg-588-returning-values.htm

XP Review for Case Study – Functions and Variables Insert a command to write the HTML code: My favorite comic is: comicbook I read it every month. Where comicbook is the text string returned by the getComic() function. The important point to understand is that comicbook is not a literal text string. It is a variable. This is standard text-book notation: words in italics, usually mean they are variables _case-review-no-function.htm 222_case-review-function-and-variable.htm 223_case-reivew-function-no-variable.htm

XP Review for Case Study – Functions and Variables Taking it one step further, if you want to take a variable and concatenate it to a text string where both double and single quotes are needed, you really need to concentrate on getting the quotes right. Let's say, instead of simply generating text output you wanted to generate an image tag like this: ; Where myComic is output from the getImage() function and is concatenated to.jpg The challenge is two-fold. First the document.write() function requires double-quotes around the whole string and the src attribute requires single quotes around the src, but you CANNOT quote variables; that is, myComic cannot be quoted _case-review-concat-variable-to-literal.htm

XP 27 Go to pg 578 in text book and continue the in-chapter exercise. Stop when you get to the end of the section on page 589

XP Sec 10.3 External JavaScript File JavaScript Comments Basic JavaScript Debugging Techniques 28

XP Accessing an External JavaScript File The code to access an external script file is: url specifies the external file that contains the scripts Place all script elements that reference external files in the document section. 29

XP Accessing an External JavaScript File 30 mpl.htm contains a element in the head section linking it to spam.js This allows mpl.htm to use functions and variables located in spam.js

XP Commenting JavaScript Code JavaScript comment symbols can be like this /* … */ or like this // … 31 Marks the rest of the line as a comment Marks the everything between the opnening and closing symbols as a comment

XP Non-JavaScript Browsers 32 Some older browsers do not support JavaScript Non-JavaScript browsers might display the JavaScript code as text in the browser. HTML and JavaScript comments are used to hide JavaScript from non-JavaScript browsers The element can be used to display alternate content for non-JavaScript browsers: JavaScript-capable browsers ignore this.

XP Debugging JavaScript Errors There are three types of errors: 1. Load-time errors – interpreter can not understand the code 2. Run-time errors – operation failed (e.g. tried to add a numeric value to a text variable.) 3. Logical errors – Load and Run OK but output is wrong (e.g. the emServer variable contains a value of “cadler” instead of “mpl.gov”) (example from in-chapter exercise) 33

XP Debugging JavaScript Programs An alert dialog box is sometimes used as a debugging aid. 34 Internet ExplorerFirefox 225_pg-599-alert-dialog.htm

XP Debugging Your JavaScript Programs If you are running Internet Explorer 8 the debugging tools are included. If you are running IE 7 you will have to download and install the free Microsoft Script Debugger from the MS Web site.  The IE 8 version seems pretty helpful, the IE 7 version, not as helpful In either case, however, you will have to make sure that Script Debugging is enabled on your browser:  Tools Menu: Internet Options: Advanced Tab: Browsing Group  Make sure that “Disable Script Debugging (Internet Explorer)” and “Disable Script Debugging (other) is not checked If you are running Firefox, when you encounter an error, you have to open the Error Console: Tools menu: Error Console Better than nothing, but not as helpful as the IE 8 debugger 35

XP Debugging Your JavaScript Programs Microsoft offers the Microsoft Script Debugger Firefox also provides the Firefox Error Console _pg-601-firefox-error-console.htm

XP The End 37