What is a Javascript ? Introduction to Java
Why use client-side
programming?
PHP already granted permission to us to create dynamic
web pages. Why also use client-side
scripting?
client-side scripting (JavaScript) benefits:
usability: can modify a page without
requiring a return post to the server (faster UI)
efficiency: can make small, quick changes
to page without waiting for server
event-driven: can respond to user actions
like clicks and key presses
server-side programming (PHP) benefits:
security: has access to server's private
data; client can't see source code
compatibility: not subject to browser
compatibility issues
power: can write files, open connections to
servers, connect to databases,
What is Javascript?
a lightweight programming language
("scripting language")
used to make web pages interactive
insert dynamic text into HTML (ex: user
name)
react to events (ex: page load user click)
get information about a user's computer
(ex: browser type)
perform calculations on user's computer
(ex: form validation)
a web standard (but not supported
identically by all browsers)
NOT related to Java other than by name
and some syntactic similarities
Javascript Vs Java
interpreted, not compiled
more relaxed syntax and rules
fewer and "looser" data types
variables don't need to be declared
errors often silent (few exceptions)
key construct is the function rather than
the class
"first-class" functions are used in many
situations
contained within a web page and
integrates with its HTML/CSS content
JavaScript vs. PHP
similarities:
both are interpreted, not compiled
both are relaxed about syntax, rules,
and types
both are case-sensitive
both have built-in regular expressions
for powerful text processing
Event-driven programming
Javascript statement
Event-driven programming
14
you are used to programs start with a
main method (or implicit main like in
PHP)
JavaScript programs instead wait for user
actions called events and respond to
them
event-driven programming: writing
programs driven by user events
Let's write a page with a clickable button
CS380 that pops up a "Hello, World" window..
Buttons
button's text appears inside tag; can also
contain images
to make a button or other UI that is responsive
1. choose the control (e.g. button) and event
(e.g. mouse 1. click) of interest
2. write a JavaScript function to run when
the event occurs
3. attach the function to the event on the
control
JavaScript functions
16
the above could be the contents of
example.js linked to our HTML page
statements placed into functions can be
evaluated in response to user events
Event handlers
JavaScript functions can be set as event
handlers
when you interact with the element, the
function will execute
onclick is just one of many event HTML
attributes we'll use
but popping up an alert window is
disruptive and annoying
A better user experience would be to have the event
DOM element objects
document.getElementById
21
document.getElementById returns the
DOM object for an element with a given
id
can alter the text within most
elements by setting the innerHTML
property
can change the text in form controls by
setting the value property
Changing element style:
variables are declared with the var
keyword (case sensitive)
types are not specified, but JS does have
types ("loosely typed")
Number, Boolean, String, Array, Object,
Function, Null, Undefined
can find out a variable's type by calling
CS380 typeof
Number type
integers and real numbers are the same
type (no int vs. double)
similar operators : + - * / % ++ -- = += -=
*= /= %=
similar precedence to Java
many operators auto-convert types: "2"
* 3 is 6
Comments (same as Java)
recall: 4 comment syntaxes
HTML: <!-- comment -->
CSS/JS/PHP: /* comment */
Java/JS/PHP: // comment
PHP: # commen
















No comments: