Lec 14
Lec 14
the WWW I
CMSC 10100-1
Winter 2004
Lecture 14
Introduction to JavaScript 1 1
Today’s Topics
• Intro to JavaScript
• A first JavaScript
• Working with variables, functions, objects
Introduction to JavaScript 1 2
Dynamic HTML (DHTML)
• Dynamic HTML can be viewed as the
combination of HTML 4 ,CSS , and JavaScript
HTML 4 represents the static structure
CSS represents the appearance details
JavaScript works on the dynamic behaviors of the
content!
• Document Object Model (DOM) provides a programming
interface between HTML/CSS and JavaScript
• DHTML isn’t really about HTML
An abstract concept of breaking up a page into
manipulable elements, and exposing them to script
Introduction to JavaScript 1 3
Why DHTML?
• Web evolves
static displays of data interactive applications
• Allows a Web page to change after loaded into
the browser
No need to communicate with server for an update
More efficient than Common Gateway Interface (CGI)
solution in certain situations
• Form validation
Introduction to JavaScript 1 4
JavaScript Language
• The JavaScript Programming Language
Scripting Languages
• Executed by an interpreter contained within the web browser
(scripting host)
• Interpreter uses a scripting engine
Converts code to executable format each time it runs
Converted when browser loads web document
Issues
• Scripting vs. Programming
• Interpreted vs. Compiled languages
• Reference:
http://www.classes.cs.uchicago.edu/classes/archive/2004/wi
nter/10100-1/02/javascript/javascript01.html
Introduction to JavaScript 1 5
JavaScript Language
Introduction to JavaScript 1 6
JavaScript Language
Introduction to JavaScript 1 7
JavaScript Language
Introduction to JavaScript 1 8
Introduction to JavaScript 1 9
JavaScript Language
Introduction to JavaScript 1 10
http://www.jsworkshop.com/js3e/list13-1.html
Introduction to JavaScript 1 11
http://www.motionnet.com/calculator/
Introduction to JavaScript 1 12
http://javascript.about.com/library/scripts/blformvalidate.htm
Introduction to JavaScript 1 13
A First JavaScript Program
Introduction to JavaScript 1 14
A First JavaScript Program
Introduction to JavaScript 1 15
A First JavaScript Program
Introduction to JavaScript 1 16
http://devedge.netscape.com/library/manuals/2000/
javascript/1.5/reference/preface.html#1003515
Introduction to JavaScript 1 17
A First JavaScript Program
Introduction to JavaScript 1 18
A First JavaScript Program
Introduction to JavaScript 1 19
A First JavaScript Program
Introduction to JavaScript 1 20
Introduction to JavaScript 1 21
Introduction to JavaScript 1 22
A First JavaScript Program
Introduction to JavaScript 1 23
http://old.jccc.net/~srao/JavaScript/Tutorial01/MyFirstJavaScript.html
Introduction to JavaScript 1 24
A First JavaScript Program
Introduction to JavaScript 1 25
A First JavaScript Program
Introduction to JavaScript 1 26
A First JavaScript Program
Introduction to JavaScript 1 27
A First JavaScript Program
Introduction to JavaScript 1 28
http://old.jccc.net/~srao/JavaScript/Tutorial01/
MultipleJavaScriptCalls.html
Introduction to JavaScript 1 29
A First JavaScript Program
Introduction to JavaScript 1 30
Introduction to JavaScript 1 31
A First JavaScript Program
Introduction to JavaScript 1 32
Introduction to JavaScript 1 33
A First JavaScript Program
Introduction to JavaScript 1 34
Introduction to JavaScript 1 35
A First JavaScript Program
Introduction to JavaScript 1 36
Working with Variables,
Functions, and Objects
• How to declare and use variables
• How to define and call functions
• About built-in JavaScript functions
• How to use JavaScript objects
• How to use object inheritance and prototypes
• How to use object methods
• About built-in JavaScript objects
• About variable scope
Introduction to JavaScript 1 37
Working with Variables
Introduction to JavaScript 1 38
Introduction to JavaScript 1 39
Introduction to JavaScript 1 40
Working with Variables
• Variables
To create:
• Use keyword var to declare the variable
• Use the assignment operator to assign the variable a value
Declare, then assign value (initialize)
• var employeeName;
• employeeName = “Ric”;
Declare and assign variable in a single statement
• var employeeName = “Ric”;
Introduction to JavaScript 1 41
Working with Variables
• Variables
Once created:
• May be changed at any point in the program
Use variable name and assignment operator
• employeeName = “Althea”;
Introduction to JavaScript 1 42
Working with Variables
• Variables
Syntax rules
• Cannot use:
Reserved words & spaces
• Must begin with one of the following:
Uppercase or lowercase ASCII letter
Dollar sign ($) or underscore (_)
• Can use numbers, but not as first character
• Variables are case-sensitive
Introduction to JavaScript 1 43
Working with Variables
• Variables
Conventions
• Use underscore or capitalization to separate
words of an identifier
employee_first_name
employeeFirstName
Introduction to JavaScript 1 44
Introduction to JavaScript 1 45
Introduction to JavaScript 1 46
Working with Variables
Introduction to JavaScript 1 47
Working with Variables
Introduction to JavaScript 1 48
Working with Functions
Introduction to JavaScript 1 49
Working with Functions
Introduction to JavaScript 1 50
Introduction to JavaScript 1 51
Working with Functions
• Calling Functions
Function invocation or call
• Statement including function name followed by a
list of arguments in parentheses
• Parameter of function definition takes on value of
argument passed to function in function call
Introduction to JavaScript 1 52
Working with Functions
• Calling Functions
Code placement
• Functions must be created (defined) before called
<head> rendered by browser before <body>
• Function definition
Place in <head> section
• Function call
Place in <body> section
Introduction to JavaScript 1 53
Working with Functions
• Variable Scope
Defines where in the program a declared variable
can be used
• Global variable
Declared outside a function and is available to all parts of the
program
var keyword optional
• Local variable
Declared inside a function and is only available within the
function it is declared
• Global and local variables can use same identifier
Introduction to JavaScript 1 54
Working with Functions
<head>
<script language=“JavaScript”>
function putStuff(myName, myNum){
document.writeln(“Hello “ + myName + “, how are you?”);
var rslt = myNum * 2;
document.writeln (myNum + “ * 2 = “, rslt);
}
</script>
</head>
<body>
<script>
putStuff(“John”, 5);
</script>
</body>
Introduction to JavaScript 1 55
Working with Functions
Introduction to JavaScript 1 56
Examples
• TwoFunctionsProgram.html
• CompanyName.html
• SingleCompanyName.html
Introduction to JavaScript 1 57
Working with Functions
Introduction to JavaScript 1 58
Built-in JavaScript Functions
alert(“some string”);
var rslt = prompt(“question”, “default”);
var rslt = confirm(“some string”);
confirm displays the string in a box and then has “ok” and
“cancel” buttons.
Returns TRUE if the user hits the “ok” button and returns FALSE if the user hits the
“cancel” button.
Introduction to JavaScript 1 59
Built-in JavaScript Functions
Introduction to JavaScript 1 60
Built-in JavaScript Functions
<head>
<title>Prompt Box</title>
Introduction to JavaScript 1 61
Built-in JavaScript Functions
Introduction to JavaScript 1 62
Introduction to JavaScript 1 63
Working with Objects
Introduction to JavaScript 1 64
Objects
Introduction to JavaScript 1 65
Working with Objects
Introduction to JavaScript 1 66
Introduction to JavaScript 1 67
Working with Objects
Introduction to JavaScript 1 69
Advanced: Working with Objects
Introduction to JavaScript 1 70
Advanced: Working with Objects
Introduction to JavaScript 1 71
Advanced: Working with Objects
Introduction to JavaScript 1 72
Advanced: Working with Objects
Introduction to JavaScript 1 73
Advanced: Working with Objects
Introduction to JavaScript 1 74
Advanced: Working with Objects
Introduction to JavaScript 1 75
Introduction to JavaScript 1 76
Advanced: Working with Objects
Introduction to JavaScript 1 77