Javascript Jeffrey Jackson
Javascript Jeffrey Jackson
JEFFREY C. JACKSON
Run on the
client’s machine
Chapter 4 not on the server
Client-Side Programming:
the JavaScript Language
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
JavaScript Introduction
Suggest the complete term a user might be entering in a
search box as he types. You can see this in action on
Google.com
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
JavaScript Introduction
• Let’s write a “Hello World!” JavaScript
program
• Problem: the JavaScript language itself has
no input/output statements(!)
• Solution: Most browsers provide standard
I/O methods
• alert: pops up alert box containing text
• prompt: pops up window where user can enter
text
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
JavaScript Introduction
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
JavaScript Introduction
• File JSHelloWorld.js:
- External Script
<script> element used
to load and execute
JavaScript code
JavaScript Introduction
• Web page and alert box generated by
JSHelloWorld.html document and
JSHelloWorld.js code:
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
JavaScript Introduction: Adding
JS to a Page
• Adding JS to your page:
1. Embedded Script:
2. External Script:
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved. 0-13-185603-0
JavaScript Introduction
• Prompt window example:
1
2
Comments
// This is a comment. It is similar to comments in C+
+ /* * This is a multiline comment in JavaScript * It is
very similar to comments in C Programming */
Local
variable
declared
within
a function
Local
declaration
shadows
corresponding
global
declaration
Output is 6
In browsers,
global
variables Output is 7
(and functions)
are stored as properties
of the window built-in object.
• A constructor is a function
• When called via new expression, a new empty Object
is created and passed to the constructor along with the
argument values
• Constructor performs initialization on object
• Can add properties and methods to object
• Can add object to an inheritance hierarchy
Produces three
alert boxes;
order of names
is implementation-dependent.
Converted to String
if necessary
o1 o2
o2 is another
name for o1
Hello
o1 o2
o1 is
changed Hello World!
o1 o2
Hello World!
ary1
length (0) Properties
toString() Inherited
sort() methods
shift()
…
ary2
length (3) Accessing array elements:
“0” (4) ary2[1]
Elements
“1” (true) ary2[“1”]
of array
“2” (“OK”) ary2.1
Must follow identifier
toString() syntax rules
…
Jackson, Web Technologies: A Computer Science
Perspective, © 2007 Prentice-Hall, Inc. All rights
reserved. 0-13-185603-0
JavaScript Arrays
• The Array constructor is indirectly called if an
array initializer is used
toString()
…
Jackson, Web Technologies: A Computer Science
Perspective, © 2007 Prentice-Hall, Inc. All rights
reserved. 0-13-185603-0
JavaScript Arrays
• Changing the number of elements:
ary2
length (2)
“0” (4)
“1” (true)
toString()
…
Jackson, Web Technologies: A Computer Science
Perspective, © 2007 Prentice-Hall, Inc. All rights
reserved. 0-13-185603-0
JavaScript Arrays
• Value of length is not necessarily the same as the
actual number of elements
var ary4 = new Array(200); Calling constructor with single argument
sets length, does not create elements
ary4
length (200)
toString()
sort()
shift()
…
toString()
splice()
Output is “number”