Professional Documents
Culture Documents
Web Programming 1
How do web pages
work again?
2
You are on
your laptop
3
Your laptop is
running a web
browser, e.g.
Chrome
4
You type a URL in
the address bar and
hit "enter"
http://bau.edu.lb
5
Server at
http://bau.edu.lb
6
Server at
http://bau.edu.lb
7
Server at
http://bau.edu.lb
The HTML will include things like
<img src="pup.png" / > and
<link src="style.css" . . . / >
which generate more requests for
those resources
8
Server at
http://bau.edu.lb
9
Finally, when all resources are loaded,
we see the loaded web page
http://bau.edu.lb
10
+ produces
11
What we've learned so far
12
What we've learned so far?
Content Structure Style Behavior
Text and HTML CSS Javascript
Images writing web pages stylistic info for php
web pages
SQL
AJAX
Interactive
dynamic
programmable
web pages
interaction with
databases
15
JavaScript
"I was under marketing orders to make it look like Java but not make it too big for
its britches ... [it] needed to be a silly little brother language." (source)
16
in the browser
17
Code in web pages
HTML can indirectly embed JavaScript files into the web page via the <scr ipt > tag.
<!DOCTYPE html>
<html>
<head>
<title>CS 1 9 3 X< /ti tl e >
<link rel="stylesheet" href="style.css" />
<script src="filename.js"></script>
</head>
<body>
... contents of the page...
</body>
</html>
18
Code in web pages
Can also be embedded internally/directly.
<!DOCTYPE html>
<html>
<head>
<title>CS 1 9 3 X< /ti tl e >
<link rel="stylesheet" href="style.css" />
<script type="text/javascript">
-- Javascript script
</script>
</head>
This is poor code quality though:
<body>
You should always separate
... contents of the page... content, presentation, and
</body> behavior
</html> 19
console.log
script.js
console.log('Hello, w o r l d ! ' ) ;
20
<head>
<title>CMPS 270 </ t it le >
< l i n k re l = " st y l e s h e e t " h r e f = " s t y l e . c s s " / >
<script src="script.js"></script>
</head>
21
<head>
<title>CMPS 270</ t it le >
< l i n k re l = " st y l e s h e e t " h r e f = " s t y l e . c s s " / >
<script src="script.js"></script>
</head>
22
<head>
<title>CMPS 270 </ t it le >
< l i n k re l = " st y l e s h e e t " h r e f = " s t y l e . c s s " / >
<script src="script.js"></script>
</head>
And the server responds with the JavaScript file, just like it
would with a CSS file or an image...
23
<head>
<title>CMPS 270 </ t it le >
< l i n k re l = " st y l e s h e e t " h r e f = " s t y l e . c s s " / >
<script src="script.js"></script>
</head>
console.log('Hello, wo r l d ! ' ) ;
24
JavaScript execution
25
first-js.html
<!DOCTYPE html>
<html>
<head>
<meta char set ="ut f - 8">
< t i t l e > F i r s t JS Example</title>
<script src="script.js"></script>
</head>
<body>
</body>
</html>
script.js
console.log('Hello, w o r l d ! ' ) ;
26
Hey, nothing happened!
27
Right-click (or control-click on Mac) and choose "Inspect"
28
Click "Console" tab 29
30
The "Console" tab is also a REPL, or an interactive language
shell, so you can type in JavaScript expressions, etc. to test
out the language.
31
Window Object
• an already available global object
• represents the currently opened
window tab in the browser
• Includes three methods for creating
dialog boxes
• alert
• prompt
• confirm
Web Programming 32
alert Dialog
alert(“hello world! \n“);
Web Programming 33
prompt Dialog
confirm(“Enter Number”,“ ”);
• Parameters
• Text message label
• Default response
Web Programming 34
Window Object
• Also includes the property (field)
• Document document
Web Programming 35
Document Object
• has a method, write, which
dynamically creates content Answer: 25
• parameter is a string
• The parameter is sent to the browser,
so it can be anything that can appear
in an HTML document (<br />, but
not \n)
script.js
document.write("Answer: " +
• More to come… result + "<br />");
Web Programming 36
JS Language Features
37
Same as Java
for-loops:
for (let i =0 ; i < 5 ; i++) { … }
while-loops:
while (notFinished) { … }
comments:
/ / comment or / * comment* /
conditionals (if statements):
if(...){
...
} else{
...
}
38
Functions
F u n c t i o n name() {
statement;
statement;
...
}
39
script.js
function h e l l o ( ) {
console.log('Hello!');
console.log('Welcome t o J a v a S c r i p t ' ) ;
}
hello();
hello();
/ / Function scope v a r i a b l e
var x = 15;
/ / Block scope v a r i a b l e
l e t f r u i t = 'banan a ';
/ / Block scope const ant ; cannot be reassigned
const isHungry = t r u e ;
You do not declare the datatype of the variable before using it ("dynamically typed")
41
What's a "block"?
A "block" or compound statement is a type of statement, which is why we can execute multiple statements
when the condition is true.
Blocks and scope
Most languages that include blocks also tie scoping rules to blocks, i.e. via "block
scope":
/ / C++ code, not JS:
i f (...) {
i n t x = 5;
...
}
/ / c a n ' t access x here
f u n c t i o n printMessage(message, times) {
f o r ( v a r i = 0 ; i < times; i + + ) {
console.log(message);
}
}
45
Understanding var
f u n c t i o n printMessage(message, t im es ) {
f o r ( v a r i = 0 ; i < t im es; i + + ) {
Q: What happens if we try to
console.log(message); print "i" at the end of
} the loop?
console.log('Value of i i s ' + i ) ;
}
printMessage('hello', 3 ) ;
The value of "i" is readable outside of
the for-loop because variables
declared with v a r have function
scope.
46
Function scope with var
var x = 10;
if (x > 0 ) {
var y = 10;
}
console.log('Value of y i s ' + y ) ;
47
Function scope with var
f u n c t i o n printMessage(message, t imes ) {
f o r ( l e t i = 0 ; i < t im es; i + + ) { Q: What happens if we
try to print "i" at the
console.log(message); end of the loop?
}
console.log('Value of i i s ' + i ) ;
}
printMessage('hello', 3 ) ;
l e t has block-scope
so this results in an
error
49
Understanding const
let x =
10;
i f (x > 0) {
const y = 10;
}
console.log(y); / / e r r o r !
const y = 10;
y = 0 ; / / er r or !
y++; / / er r or !
const l i s t = [ 1 , 2 , 3 ] ;
list.push(4); / / OK
51
Contrasting with l e t
l e t y = 10;
y = 0 ; / / OK
y++; / / OK
l e t l i s t = [1, 2, 3];
list.push(4); / / OK
52
Variables best practices
• You will see a ton of example code on the internet with var since const and
l e t are relatively new.
• However, const and l e t are well-supported, so there's no reason not to
use them.
• (This is also what the Google and AirBnB JavaScript Style Guides
recommend.)
53
Types
JS variables do not have types, but the values do.
There are also Object types, including Array, Date, String ,etc.
54
Numbers
const homework= 0 . 4 5 ;
const midterm = 0 . 2 ;
const f i n a l = 0.35;
const score =
homework * 87 + midterm * 90 + f i n a l * 95;
c o n s o l e . l o g( s c o r e ) ; / / 90.4
55
Strings
l e t snack=' c o o ' ;
snack += ' k i e s ' ;
snack = snack.toUpperCase();
c o n s o l e . l o g ( " I want " + snack);
56
Strings
String properties nd methods:
Web Programming 57
Boolean
l e t isHungry = t r u e ;
l e t isTeenager = age > 12 && age < 20;
58
Boolean
i f (username) {
/ / username i s defined
}
59
Equality
60
Equality
Instead of fixing == and ! = , the ECMAScript standard kept the existing behavior
but added === and !==
62
Null and Undefined
63
Arrays
- 0-based indexing
- Can check size via length property similar to java
64