Chapter 7 JavaScript: Introduction to Scripting. Outline Simple Programs Objects and Variables Obtaining User Input with prompt Dialogs – –Dynamic Welcome.

Slides:



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

JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
Java Script Session1 INTRODUCTION.
Javascript It’s not JAVA. What do these all have in common?
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
The Web Warrior Guide to Web Design Technologies
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Introduction to Scripting.
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 Document Object Model. How to use JavaScript  JavaScript can be embedded into your html pages in a couple of ways  in elements in both and.
Information Technology Center Hany Abdelwahab Computer Specialist.
Guide To UNIX Using Linux Third Edition
Introduction to scripting
4.1 JavaScript Introduction
JavaScript: Control Structures September 27, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel,
 2004 Prentice Hall, Inc. All rights reserved. Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
CS346 - Javascript 1, 21 Module 1 Introduction to JavaScript CS346.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
1 CS101 Introduction to Computing Lecture 18 Objects, Properties, Methods (Web Development Lecture 6)
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
DHTML: Dynamic HTML Internet Technology1. What is DHTML? A collection of enhancements to HTML ► To create dynamic and interactive websites Combination.
 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.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Using Client-Side Scripts to Enhance Web Applications 1.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
JavaScript - A Web Script Language Fred Durao
JS Basics 1 Lecture JavaScript - Basics. JS Basics 2 What is JavaScript JavaScript is a “simple”, interpreted, programming language with elementary object-
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
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.
 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.
Introduction to JavaScript Objects, Properties, Methods.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
1 Chapter 3 – JavaScript Outline Introduction Flowcharts Control Structures if Selection Structure if/else Selection Structure while Repetition Structure.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
HTML Overview Part 5 – JavaScript 1. Scripts 2  Scripts are used to add dynamic content to a web page.  Scripts consist of a list of commands that execute.
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.
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.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing a Line of Text in a Web Page 7.3 Another JavaScript.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 4 Java Script - Part1. 2 Outlines Introduction to Java Script Variables, Operators and Functions Conditional statements JavaScript Objects Forms.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
REEM ALMOTIRI Information Technology Department Majmaah University.
JavaScript: A short introduction Joseph Lee Created by Joseph Lee.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
Module 1 Introduction to JavaScript
Chapter 6 JavaScript: Introduction to Scripting
Chapter 13 - JavaScript/JScript: Introduction to Scripting
Introduction to Scripting
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
14 A Brief Look at JavaScript and jQuery.
Chapter 7 - JavaScript: Introduction to Scripting
DHTML Javascript Internet Technology.
JavaScript: Introduction to Scripting
WEB PROGRAMMING JavaScript.
DHTML Javascript Internet Technology.
T. Jumana Abu Shmais – AOU - Riyadh
Chapter 7 - JavaScript: Introduction to Scripting
JavaScript: Introduction to Scripting
Chapter 7 - JavaScript: Introduction to Scripting
Chapter 7 - JavaScript: Introduction to Scripting
Chapter 7 - JavaScript: Introduction to Scripting
Presentation transcript:

Chapter 7 JavaScript: Introduction to Scripting

Outline Simple Programs Objects and Variables Obtaining User Input with prompt Dialogs – –Dynamic Welcome Page – –Adding Integers Arithmetic Decision Making: Equality and Relational Operators

Introduction JavaScript is an interpreted, scripted, programming language JavaScript is an interpreted, scripted, programming language –Core syntax resembles C++ and Java –has some object-oriented capabilities Web browsers contain a JavaScript interpreter Web browsers contain a JavaScript interpreter Use JavaScript to Use JavaScript to –Enhances functionality and appearance –Makes pages more dynamic and interactive –etc…

JavaScript JavaScript is case sensitive JavaScript is case sensitive Statements in JavaScript end in a semi- colon Statements in JavaScript end in a semi- colon JavaScript code is often embedded in the section of the document JavaScript code is often embedded in the section of the document JavaScript code in the section is executed before any XHTML code in the of the document JavaScript code in the section is executed before any XHTML code in the of the document

JavaScript Use the element to embed JavaScript code within your XHTML: Use the element to embed JavaScript code within your XHTML: –e.g. document.writeln (" Welcome to COSC 2956 ") document.writeln (" Welcome to COSC 2956 ")</script> – type attribute Specifies the type of file and the scripting language use Specifies the type of file and the scripting language use – writeln method Write a line in the document Write a line in the document

JavaScript JavaScript code can be shielded with XHTML comments: JavaScript code can be shielded with XHTML comments: – – –needed for proper validation <!-- // our first JavaScript code snippet document.title = "Welcome to COSC 2956"; // --> </script> Comments within JavaScript code can be: Comments within JavaScript code can be: –single-line style: // my single-line comment –multi-line style: /* … my multi-line comment... */

Objects An object consists of An object consists of –properties (attributes) –methods (behaviour) Properties define the characteristics of an object Properties define the characteristics of an object –e.g., a browser document has a 'title' property Methods define the functions that an object can perform Methods define the functions that an object can perform –e.g., a browser window can be resized programmatically by using the resizeTo( ) method

Objects To use a property of an object, use the following syntax: objectName.propertyName To use a property of an object, use the following syntax: objectName.propertyName –For example: document.title = "My title " window.defaultStatus = "Everything is A-OK" To use a method of an object, use the following syntax: objectName.methodName( ) To use a method of an object, use the following syntax: objectName.methodName( ) –For example, to resize the browser width and height: window.resizeTo( 600, 400 )

The document Object Every window object has a document object, that allows you to access properties and methods of the document currently loaded into the browser Every window object has a document object, that allows you to access properties and methods of the document currently loaded into the browser Some document properties: Some document properties: –bgColor, fgColor - background and foreground colour of the document –alinkColor, linkColor, vlinkColor - colours for hypertext links –URL - the URL of the current document –referrer - the URL of the document with the link that loaded this document –title - the title of the document –lastModified - the modification date of the document Some document methods Some document methods –write( ) –writeln( )

document.writeln( ) Use the document.writeln( ) method to dynamically generate XHTML code Use the document.writeln( ) method to dynamically generate XHTML code Pass writeln( ) the XHTML code that is to be rendered, as a quoted string; e.g., Pass writeln( ) the XHTML code that is to be rendered, as a quoted string; e.g., –document.writeln(" First Heading "); writeln( ) writes a line of XHTML markup and positions the output cursor at the beginning of the next line (of the code being generated, not the rendered document) writeln( ) writes a line of XHTML markup and positions the output cursor at the beginning of the next line (of the code being generated, not the rendered document) XHTML validator does not catch errors in HTML embedded in JavaScript XHTML validator does not catch errors in HTML embedded in JavaScript

The window Object Every web browser window is represented by a window object Every web browser window is represented by a window object A window object defines properties and methods that allow you to manipulate the web browser window A window object defines properties and methods that allow you to manipulate the web browser window Some window properties: Some window properties: –defaultStatus - text that appears in the status line of the browser Some window methods: Some window methods: –alert( ), confirm( ), prompt( ) - simple dialog boxes –close( ) - close the window –open( ) - open a new top-level window to display a document –print( ) - print the contents of the window –resizeBy( ), resizeTo( ) - resize the window –moveTo( ) - move the window

Variables Use a variable to store, manipulate and retrieve a value from memory during program execution Use a variable to store, manipulate and retrieve a value from memory during program execution A variable can hold a variable of any type A variable can hold a variable of any type –x = 12; –x = "123"; Declare variables with the var keyword Declare variables with the var keyword var x; var y = "abd"; var myInt = 1, myFloat = 2.4; Variables that are declared but not initialized are undefined. Trying to print them produces the string literal "undefined" Variables that are declared but not initialized are undefined. Trying to print them produces the string literal "undefined" Attempting to read the value of a variable that is not declared causes a run-time error Attempting to read the value of a variable that is not declared causes a run-time error

welcome5.html (1 of 2)

Fig. 7.7Prompt dialog displayed by the window objects prompt method. This is the prompt to the user. This is the default value that appears when the dialog opens. This is the text field in which the user types the value. When the user clicks OK, the value typed by the user is returned to the program as a string.

Example: Adding Integers Prompt user for two integers and calculate the sum (Fig. 7.8) Prompt user for two integers and calculate the sum (Fig. 7.8) parseInt parseInt –Converts its string argument to an integer

Addition.html (1 of 2)

Addition.html (2 of 2)

Arithmetic Many scripts perform arithmetic calculations Many scripts perform arithmetic calculations –Expressions in JavaScript must be written in straight-line form

Equality and Relational Operators Decision based on the truth or falsity of a condition Decision based on the truth or falsity of a condition –Equality operators –Relational operators

welcome6.html (1 of 3)

welcome6.html (2 of 3)

Operators Summary