U2._Introduction_to_Javascript
U2._Introduction_to_Javascript
CS380
Why use client-side
8
programming?
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
9
programming?
server-side programming 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, ...
How to add JavaScript to
10
HTML
Internal JS: We can add JavaScript directly to our
HTML file by writing the code inside the <script> tag.
The <script> tag can either be placed inside the
<head> or the <body> tag according to the
requirement.
External JS: We can write JavaScript code in other
file having an extension.js and then link this file inside
the <head> tag of the HTML file in which we want to
add this code.
Linking to a JavaScript file:
11
script
<script src="filename" type="text/javascript"></script>
+
Description
Addition
Example
x=2
Result
4
y=2
x+y
- Subtraction x=5 3
y=2
x-y
* Multiplication x=5 20
y=4
x*y
/ Division 15/5 3
5/2 2,5
% Modulus (division 5%2 1
remainder)
10%8 2
10%2 0
++ Increment x=5 x=6
x++
-- Decrement x=5 x=4
x--
JavaScript Operators – 2
Assignment Operators Operator Example Is The Same As
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
%= x%=y x=x%y
JavaScript Operators - 3
Comparison Operator
==
Description
is equal to
Example
x===y returns
false
y=3
|| or x=6
y=3
(x==5 || y==5)
returns false
! not x=6
y=3
!(x==y) returns
true
JavaScript Operators - 6
Special Operators
Alert Box
20
alert("message"); // message
confirm("message"); // returns true or false
prompt("message"); // returns user input string
JS
Prompt Box
23
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
Document Object Model
29
(DOM)
The document object represents the whole
html document.
When html document is loaded in the browser, it
becomes a document object.
It is the root element that represents the html
document. It has properties and methods.
By the help of document object, we can add
dynamic content to our web page.
Document Object Properties
30
document.form1.name.valu
e
Document Object Model
31
(DOM)
most JS code
manipulates elements
on an HTML page
we can examine
elements' state
e.g. see whether a box
is checked
we can change state
e.g. insert some new
text into a div
we can change styles
DOM element objects
32
Methods of Document
33
Object
Accessing elements:
34
document.getElementById
JS
function changeText() {
var span = document.getElementById("output");
var textBox = document.getElementById("textbox");
textbox.style.color = "red";
}
JS
Accessing elements:
35
document.getElementById
document.getElementById returns the
DOM object for an element with a given
id
can change the text inside most
elements by setting the innerHTML
property
can change the text in form controls by
setting the value property
Changing element style:
36
element.style
Property or style
Attribute
object
color color
padding padding
background-color backgroundColor
border-top-width borderTopWidth
Font size fontSize
Font famiy fontFamily
Preetify
37
function changeText() {
//grab or initialize text here
// 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)
48
while (condition) {
statements;
} JS
do {
statements;
} while (condition);
JS
Syntax:
/pattern/modifier(s);
Example:
expression
Regular Expression-
56
Methods
Method Description
Executes a search for a match in a string. It returns an array of information
exec()
or null on a mismatch.
test() Tests for a match in a string. It returns true or false.
Returns an array containing all of the matches, including capturing groups,
match()
or null if no match is found.
matchAll() Returns an iterator containing all of the matches, including capturing groups.
Tests for a match in a string. It returns the index of the match, or -1 if the search
search()
fails.
Executes a search for a match in a string, and replaces the matched substring with
replace()
a replacement substring.
Executes a search for all matches in a string, and replaces the matched substrings
replaceAll()
with a replacement substring.
Uses a regular expression or a fixed string to break a string into an array of
split()
substrings.
Regular Expression-
57
Modifiers
Modifiers are used to perform case-insensitive and
global searches:
g- Perform a global match (find all matches
Examples:
[abc] Any of the characters a, b, or c
[A-Z] Any character from uppercase A to
uppercase Z
[a-z] Any character from lowercase a to lowercase
z
[A-z] Any character from uppercase A to lowercase
Regular Expression- Groups
59
brackets
digit)