Intro to Javascript
Unit-4
Client Side Scripting
2
CS380
Why use client-side
3
programming?
PHP already allows us to create dynamic
web pages. Why also use client-side
scripting?
client-side scripting (JavaScript) benefits:
usability: can modify a page without
having to post back 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
Why use client-side
4
programming?
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?
5
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)
What is Javascript?
6
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
7
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 Java
8
+ =
JavaScript vs. PHP
9
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
JavaScript vs. PHP
10
differences:
JS is more object-oriented: noun.verb(),
less procedural: verb(noun)
JS focuses on user interfaces and
interacting with a document; PHP is
geared toward HTML output and
file/form processing
JS code runs on the client's browser; PHP
code runs on the web server
JS <3
Linking to a JavaScript file:
11
script
<script src="filename" type="text/javascript"></script>
HTML
script tag should be placed in HTML
page's head
script code is stored in a separate .js file
JS code can be placed directly in the
HTML file's body or head (like CSS)
but this is bad style (should separate
content, presentation, and behavior
Event-driven programming
12
split breaks apart a string into an array
using a delimiter
can also be used with regular expressions
(seen later)
join merges an array into a single string,
placing a delimiter between them
A JavaScript statement:
13
alert
alert("IE6 detected. Suck-mode enabled.");
JS
a JS command that pops up a dialog box
with a message
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
that pops up a "Hello, World" window...
Buttons
15
<button>Click me!</button> HTML
button's text appears inside tag; can
also contain images
To make a responsive button or other UI
control:
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
function name() {
statement ;
statement ;
...
statement ;
} JS
function myFunction() {
alert("Hello!");
alert("How are you?");
} JS
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
CS380
Event handlers
17
<element attributes onclick="function();">...
HTML
<button onclick="myFunction();">Click me!</button>
HTML
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