1 JavaScript and Dynamic Web Pages Lecture 7. 2 Static vs. Dynamic Pages  A Web page uses HTML tags to identify page content and formatting information.

Slides:



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

The Web Warrior Guide to Web Design Technologies
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 HCI 201 JavaScript - Part 1. 2 Static web pages l Static pages: what we have worked with so far l HTML tags tell the browser what to do with the content.
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.
CIS101 Introduction to Computing Week 09 Spring 2004.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
Introduction to JavaScript. Aim To enable you to write you first JavaScript.
Introduction to scripting
1 Events Lect 8. 2 Event-driven Pages one popular feature of the Web is its interactive nature e.g., you click on buttons to make windows appear e.g.,
Javascript and the Web Whys and Hows of Javascript.
Adding JavaScript (<script tag>)
CS346 - Javascript 1, 21 Module 1 Introduction to JavaScript CS346.
 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,
Chapter 4 JavaScript and Dynamic Web pages. Objectives Static Web pages Dynamic Web pages JavaScript Variables Assignments. JavaScript Functions –(prompt(“”,””)
Objective Static vs. Dynamic Web pages. Variables. Assignments. JavaScript Hierarchy of Objects JavaScript Functions (prompt(“”,””) Document.write(“”)
Computers and Scientific Thinking David Reed, Creighton University JavaScript and User Interaction 1.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Dynamic Web Pages & JavaScript. Dynamic Web Pages Dynamic = Change Dynamic Web Pages are web pages that change. More than just moving graphics around.
1 Functions Lecfture Abstraction abstraction is the process of ignoring minutiae and focusing on the big picture in modern life, we are constantly.
Introduction.  The scripting language most often used for client-side web development.  Influenced by many programming languages, easier for nonprogrammers.
1 CSC 121 Computers and Scientific Thinking David Reed Creighton University JavaScript and Dynamic Web Pages.
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
Introduction to JavaScript CS101 Introduction to Computing.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
1 A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 4 JavaScript and.
Sahar Mosleh California State University San MarcosPage 1 JavaScript Basic.
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.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Making dynamic pages with javascript Lecture 1. Java script java versus javascript Javascript is a scripting language that will allow you to add real.
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.
CIS 3.5 Lecture 2.3 "Introduction to 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.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Scripting with Client-Side Processing Scripting with Client Side Processing Lesson – Web Technologies Copyright © Texas Education Agency, All rights.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
CSC 121 Computers and Scientific Thinking Fall Interactive Web Pages.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
CSC 121 Computers and Scientific Thinking Fall Event-Driven Programming.
JavaScript 101 Lesson 6: Introduction to Functions.
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
REEM ALMOTIRI Information Technology Department Majmaah University.
1 CSC 121 Computers and Scientific Thinking David Reed Creighton University JavaScript and Dynamic Web Pages.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
JavaScript: A short introduction Joseph Lee Created by Joseph Lee.
Web Programming Java Script-Introduction. What is Javascript? JavaScript is a scripting language using for the Web. JavaScript is a programming language.
JavaScript Part 1 Introduction to scripting The ‘alert’ function.
Module 1 Introduction to JavaScript
CHAPTER 10 JAVA SCRIPT.
Chapter 6 JavaScript: Introduction to Scripting
Introduction to Scripting
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
Intro to PHP & Variables
WEB PROGRAMMING JavaScript.
Chapter 4 JavaScript and Dynamic Web Pages
T. Jumana Abu Shmais – AOU - Riyadh
JavaScript is a scripting language designed for Web pages by Netscape.
JavaScript: Introduction to Scripting
Presentation transcript:

1 JavaScript and Dynamic Web Pages Lecture 7

2 Static vs. Dynamic Pages  A Web page uses HTML tags to identify page content and formatting information.  HTML can produce only static pages that look the same each time they are loaded into a browser and don’t change.  In 1995, researchers at Netscape developed JavaScript, a language for creating dynamic pages. They can change their appearance.  over time (e.g., a different image each time that a page is loaded), or  in response to a user’s actions (e.g., typing, mouse clicks, mouse over, etc). The page is now interactive.

3 JavaScript  JavaScript is a programming language a programming language is a language for specifying instructions that a computer can execute each statement in a programming language specifies a particular action that the computer is to carry out (e.g., changing an image or opening a window when a button is clicked)

JavaScript 4 JavaScript was developed for the purpose of adding dynamic content to Web pages add JavaScript statements to a Web page between the HTML tags when the browser displays the page, the statements inside the SCRIPT tags are interpreted (translated into machine language at the time of usage) and executed.

JavaScript  The language instructions are written in lower case.  All instructions must be spelled correctly or the interpreter will not recognize them.  Parts of a instructions need to be separated by space and not run together.  The correct punctuation must be used. Every statement ends with a semicolon  JavaScript is fussier than HTML.

String Literal  A string literal is a sequence of characters enclosed in quotes.  Any character may be included  A string literal must be written on one line.  For example  “Dave”  “This is also a string!”

Alert() alert(“Got your attention"); The alert() function (yet to be defined) asks the browser to pop-up a small window that contains the contents of the string (without the quotes).  alert must be all lower case letters.  The string may contain any letters (upper or lower case), numbers, or punctuation.  A button labeled “OK" appears in the window. When the user clicks it, the window disappears.

You try it. o Type into body of new or existing web page. Don’t copy and paste. o Remember script tags alert("my message"); 8

Variable  a variable is a name used to store a value.  The value can be changed during the program. Thus the name variable.  For example: number name  The name should be meaningful— related to the value stored

Assignment Statement  An assignment statement assigns a value to the variable. number=7; number=6+5; First the value 7 is assigned to the variable number. Then the value of number is changed to ??. Note the semi-colons after each statement.

Assignment statements firstName = “Dave”;  We use lower case letters for variable names.  To show that the name of the variable consists of two words, we sometimes capitalize the first letter of the second word.

12 Variables & Memory Cells  computers keep track of the value stored in a variable by associating each variable with a specific piece of memory which we call a memory cell, an imaginary box in which the value is stored  when firstName = “Dave”; is executed, the value (e.g., "Dave" ) is stored in a memory cell associated with the variable firstName  The value may be changed.  any reference to the variable name looks up the value stored in its associated memory cell  What would alert(firstName); produce? Why?

13 Assignment Statement using prompt firstName = prompt("Please enter your name", ""); when an assignment statement involving prompt is executed by the browser a separate window with a text box is opened when the user is done typing, he clicks the OK button the text that was entered is assigned to the variable In this example, the variable firstName stores the value Dave

14 Assignment Statement (cont.)  the general form of an assignment statement using a prompt VARIABLE = prompt("PROMPT MESSAGE", "");  here, the variable is used to store the user's first name, so firstName is a meaningful name  here, the message "Please enter your name" tells the user what is expected

You do it. o Don’t copy and paste. name = prompt("Enter your name: ", ""); alert(name); 15

16 Assignment Statement using prompt Another form: lastName = prompt("Please enter your last name"); Note there is no second set of quotes. The word “undefined” appears in the text box in the prompt window in Internet Explorer.

17 Prompts with Defaults  Yet another form: sometimes it makes sense to provide default values for prompts can specify a string instead of second "" this string will appear in the prompt text box if the user wants to accept this default value, he can just click OK  We wanted to create a page that displays a verse of the children's song, Old MacDonald had a Farm.

18 Prompts with Defaults (cont.)  the page should be able to display any verse  can accomplish this by prompting the user for the animal and sound animal = prompt("Enter a kind of animal:", "cow"); sound = prompt("What kind of sound does it make?", "moo");  Why did I go to a new line after sound= ?  Why two different variable names?

19 Prompts with Defaults (cont.) the default values automatically appear in the prompt boxes the user can click OK to accept the defaults OR type new values into the prompt box

You try it 20

21 Document.write(); document.write("Hello !"); writes the string (without the quotes) onto your web page would be displayed by the browser as Hello !

Assignment  Recall: The programmer can assign values to variables to be stored for use by the program: firstName=“Dave”; document.write(firstName);  The value stored in firstname would be displayed by the browser as Dave

+ in JavaScript, the + operator serves two purposes when applied to numbers, + means addition when applied to strings, + means concatenation—attach the two strings to each other  word1=“book”  word2=word1+”worm”  word2 is “bookworm”

24 Write Statement when a write statement is executed by the browser the message specified in the statement is written into the HTML page a message can include  a string literal  a variable  a combination of strings and variables, connected via '+' firstName=“Dave”; document.write("Hello " + firstName + ", welcome to my Web page."); when a variable is encountered, the browser substitutes the value currently assigned to that variable Note: Space inside quote is kept. Space outside quote is ignored

25 Write Statement (cont.) the general form of a write statement is document.write("MESSAGE TO BE DISPLAYED " + VARIABLE + " MORE MESSAGE" +...); note that the statement can be broken across lines, as long as no string literal is split. (i.e., the beginning and ending quotes of a string must be on same line.) the pieces of the message are displayed in sequence, with no spaces in between  if you want spaces, you have to enter them in the text

26 Formatted Output the output produced by a write statement is embedded in the page the browser displays this output just as it does any other text if the text contains HTML tags, the browser will interpret the tags and format the text accordingly firstName ="Dave“; document.write("Hello " + firstName + ", welcome to my Web page."); the browser would execute the statement to produce Hello Dave, welcome to my Web page. which would be displayed by the browser as ?? Hello Dave, welcome to my Web page.

27 Simple Dynamic Page below is a simple Web page with dynamic content note: dynamic content can be mixed with static HTML content it demonstrates two types of JavaScript statements an assignment statement that asks the user for input and stores that input a write statement that writes text into the HTML page

You try it 28

29 bgColor  we can change the background color of a page after it has been downloaded: document.bgColor = “red”;  The user can choose a color: color = prompt(“red, white or blue?”); document.bgColor = color;  will change the background color of the page to whatever the user chooses. (trace the two steps.)  be careful that the text will still be visible.

You try it 30

31 Syntax  Syntax is the rules about punctuation, spelling, etc. that a program must follow.  JavaScript is fussier than HTML: Case counts – use lowercase for instructions Spelling counts Spacing counts – separate words Punctuation counts – all statements end with semicolons

32 Syntax Errors  An error in the format of a JavaScript statement is known as a syntax error document.write("This example is illegal since the string is broken across lines"); yields: Error: unterminated string literal document.write("The value of x is " x); yields: Error: missing ) after argument list What is the actual error?

33 Variable Name Rules a variable name can be any sequence of letters, digits, and underbars (but must start with a letter) valid: tempInFahr current_age r2d2 Sum2Date x invalid: 2b_or_not2b salary$ "sum_to_date" two words (meant as one variable) variable names are case sensitive, so Sum and SUM are treated as different variables

34 Another Example Recall: once you create a variable, you can repeatedly assign values to it Thus the name “variable” only the most recent value is retained in memory EXAMPLE: suppose we want to prompt the user for two different foods if only one food is needed at a time, we can reuse the same variable

35 Reusing Variables food = prompt("What is your favorite food?", ""); document.write("Your favorite food is " + food + " "); the first pair of statements stores the user's favorite food displays that food in the page

36 Reusing Variables (cont.) food = prompt("What is your least favorite food?", ""); document.write("Your least favorite food is " + food + " "); the second pair of statements stores the user's least favorite food (overwriting the old value) displays that food in the page