Unit 3-Javascript (1)
Unit 3-Javascript (1)
Faculty:
Dr. Narinder Kaur
Seera
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 is -
interpreted, not compiled
document.write("Hello World");
JavaScript has Events and event handlers
Exception handling in JavaScript is almost the
same as in Java
JavaScript has some features unlike
anything in Java:
Variable names are untyped: the type of a
variable depends on the value it is currently
holding
Objects and arrays are defined in quite a
JavaScript vs. PHP
8
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
9
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
About JavaScript
JavaScript is not Java, or even related to Java
The original name for JavaScript was “LiveScript”
The name was changed when Java became popular
Statements in JavaScript resemble statements in
Java, because both languages borrowed heavily
from the C language
JavaScript should be fairly easy for Java programmers
to learn
However, JavaScript is a complete, full-featured,
complex language
JavaScript is seldom used to write complete
“programs”
Instead, small bits of JavaScript are used to add
functionality to HTML pages
JavaScript is often used in conjunction with HTML
Using JavaScript in a
browser
JavaScript code is included within <script>
tags:
– <script type="text/javascript">
document.write("<h1>Hello World!</h1>") ;
</script>
Notes:
The type attribute is to allow you to use other
scripting languages (but JavaScript is the default)
This simple code does the same thing as just putting
<h1>Hello World!</h1> in the same place in the
HTML document
The semicolon at the end of the JavaScript statement
is optional
You need semicolons if you put two or more statements
on the same line
Where to put JavaScript
JavaScript can be put in the <head> or in the
<body> of an HTML document
JavaScript functions should be defined in the <head>
This ensures that the function is loaded before
it is needed
JavaScript in the <body> will be executed as the page
loads
JavaScript can be put in a separate .js file
– <script src="myJavaScriptFile.js"></script>
Put this HTML wherever you would put the actual
JavaScript code
An external .js file lets you use the same JavaScript on
multiple HTML pages
The external .js file cannot itself contain a <script> tag
Linking to a JavaScript file:
13
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
Primitive data types
JavaScript has three “primitive” types: number,
string, and boolean
Everything else is an object
Numbers are always stored as floating-point
values
Hexadecimal numbers begin with 0x
Some platforms treat 0123 as octal, others treat it as
decimal
Strings may be enclosed in single quotes or
double quotes
Strings can contains \n (newline), \" (double quote),
etc.
Booleans are either true or false
Variables
Variables are declared with a var
statement:
– var pi = 3.1416, x, y, name = "Dr. Dave" ;
Variables names must begin with a letter or
underscore
Variable names are case-sensitive
Variables are untyped (they can hold values
of any type)
The word var is optional (but it’s good style to
use it)
Variables declared within a function are
local to that function (accessible only
Variables
16
// single-line comment
/* multi-line comment */
JS
var sum = 0;
for (var i = 0; i < 100; i++) {
sum = sum + i;
}
JS
var s1 = "hello";
var s2 = "";
for (var i = 0; i < s.length; i++) {
s2 += s1.charAt(i) + s1.charAt(i);
}
// s2 stores "hheelllloo"
JS
while loops (same as Java)
28
while (condition) {
statements;
} JS
do {
statements;
} while (condition);
JS
Arrays
Strings
Date Object
Math Object
Document Object
Browser Object Model’s Object (BOM’s
objects)
Window
History
Screen
Arrays
35
function myFunction(str)
{ //function definition
alert("Hello!“+str);
alert("How are you?");
}
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
A JavaScript statement:
71
alert
alert(“Access Denied.");
JS
alert("message"); // message
confirm("message"); // returns true or false
prompt("message"); // returns user input string
JS
73
THANK
YOU