Intro Javascript
Intro 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:
+ =
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
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