JavaScript 1 Core Syntax
JavaScript 1 Core Syntax
JavaScript:
A Crash Course
Servlets, Spring,
JSP, JSF
2.0, Struts,
Ajax,
GWT
2.0,Ruby/Rails
Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Hibernate/JPA,
EJB3,
Web
Services,
Contact
hall@coreservlets.com
for details
Developed and taught by well-known
author
and developer. At public
venues or onsite at your location.
Overview
JavaScript references
Embedding in browser
HTML versions
Basic syntax
Arrays
Strings and regular expressions
Intro
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Example (loading-scripts.html)
<!DOCTYPE ...><html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Loading Scripts</title>
...
Loads script from previous page
<script src="./scripts/phish.js"
type="text/javascript"></script>
</head>
<body>
Calls showWinnings1 when user presses
...
button. Puts result in dialog box.
<input type="button" value="How Much Did You Win?"
onclick='showWinnings1()'/>
...
<script type="text/javascript">showWinnings2()</script>
...
</body></html>
/
/
Calls showWinnings2 when page is loaded in
browser. Puts result at this location in page.
12
Example (Results)
13
Summary
XHTML
Most common version used with Ajax apps or Dynamic
HTML apps (JavaScript apps that manipulate the DOM)
Follows XML syntax
syntax, lowercase tags
HTML 4
Very common in non
non-JavaScript
JavaScript apps
Not recommended for Ajax apps
16
XHTML
Summary
F
Follows
ll
XML syntax. Lowercase
L
tags, endd tags required,
i d
quotes around attribute values.
Basic structure
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www
xmlns http://www.w3.org/1999/xhtml
w3 org/1999/xhtml">>
<head><title></title></head>
<body> </body></html>
Pros
Code corresponds very directly to internal (DOM)
representation by the browser
Cons
DOCTYPE and <html> start tag are long and tedious
17
Pseudo-HTML 5
Summary
F
Follows
ll
XML syntax. XHTML ((transitional)
i i l) syntax but
b
with simpler DOCTYPE and <html> start tag.
Basic structure
<!DOCTYPE html>
<html>
<head><title> </title></head>
<head><title></title></head>
<body> </body></html>
Pros
C
Code
d corresponds
d very di
directly
l to internal
i
l (DOM)
representation by the browser
Cons
Not strictly compliant with spec. May get warnings from
formal validators, especially with non-CSS formatting.
18
HTML 4
Summary
Does not follow XML syntax. Tags not case sensitive. End
tags and quotes on attribute values sometimes optional.
Basic structure
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD><TITLE> </TITLE></HEAD>
<HEAD><TITLE></TITLE></HEAD>
<BODY> </BODY></HTML>
Pros
Simple code. Widely used in non-Ajax apps.
Cons
19
S
Source code
d and
d iinternal
t
l browser
b
representation
t ti can be
b
substantially different, requiring mental translation when
thinking of how to manipulate DOM from JavaScript.
Basic
JavaScript
p Syntax
y
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Variables
Introduce with var
For global variables (!) and local variables.
No var for function arguments
Statements
Semicolons are technically optional
But highly recommended
Consider
return x
return
x
They are not identical! The second one returns, then evaluates
x. You should act as though semicolons are required as in Java.
Comments
Same as in Java (/* ... */ and // ...)
22
while loop
Same as Java except test can be converted to boolean
while(someTest) { doLoopBody(); }
do/while loop
23
for/in loop
On surface, looks similar to Java for/each loop, but
For arrays, values are array indexes, not array values
Use this loop for objects (to see property names), not arrays!
Fails with Prototype or other extended arrays
Functions
Math.abs, Math.acos, Math.asin, Math.atan, Math.atan2,
Math.ceil, Math.cos, Math.exp, Math.floor, Math.log,
Math.max, Math.min, Math.pow, Math.random,
Math.round, Math.sin, Math.sqrt, Math.tan
Constants
Math.E, Math.LN10, Math.LN2, Math.LOG10E,
Math.PI, Math.SQRT1_2, Math.SQRT2
25
Arrays
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Array Basics
One-step array allocation
var primes = [2, 3, 5, 7, 11, 13];
var names = ["Joe", "Jane", "John", "Juan"];
No trailing comma after last element (see later slide)
Indexed at 0 as in Java
for(var
o (va i=0;
0; i<names.length;
a es. e gt ; i++)) {
doSomethingWith(names[i]);
}
27
for-in loop
28
More on Arrays
Arrays can be sparse
var names = new Array();
A
()
names[0] = "Joe";
names[100000] = "Juan";
Arrays Example
function arrayLoops() {
var names =
["Joe", "Jane", "John"];
printArray1(names);
printArray2(names);
names.length = 6;
printArra 1(names)
printArray1(names);
printArray2(names);
}
function printArray1(array) {
for(var i=0; i<array.length; i++) {
console.log("[printArray1] array[%o] is %o", i, array[i]);
}
}
console.log is a printf-like way to print output in Firebug
30
Strings and
Regular
g
Expressions
p
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
String Basics
You can use double or single quotes
var names = [["Joe"
Joe , 'Jane'
Jane , "John"
John , 'Juan'];
Juan ];
Numbers
N b
can be
b converted
t d to
t strings
ti
Automatic conversion during concatenations.
var val = 3 + "abc" + 5; // Result is "3abc5"
Conversion
C
i with
ith fi
fixedd precision
ii
var n = 123.4567;
var val = n.toFixed(2); // Result is 123.46 (not 123.45)
HTML methods
anchor, big, bold, fixed, fontcolor, fontsize, italics, link,
small, strike, sub, sup
"test".bold().italics().fontcolor("red") returns
'<font color="red"><i><b>test</b></i></font>'
Regular Expressions
You specify a regexp with /pattern/
Not
N with
i h a String
S i as in
i Java
J
^,, $,, .
\
*, +, ?
{n} {n,}
{n},
{n }
[]
\s, \S
\w \W
\w,
beginning,
g
g, end of string,
g, anyy one char
escape what would otherwise be a special character
0 or more, 1 or more, 0 or 1 occurrences
exactly n,
n n or more occurrences
grouping
whitespace, non-whitespace
word char (letter or number),
number) non-word
non word char
Modifiers
/pattern/g do global matching (find all matches, not just first one)
/pattern/i do case-insensitive matching
/pattern/m do multiline matching
34
35
JavaScript
p Regular
g
Expression
p
Tutorials
http://www.evolt.org/article/Regular_Expressions_in_
JavaScript/17/36435/
http://www.javascriptkit.com/javatutors/re.shtml
h //
j
i ki
/j
/ h l
36
Wrap-up
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Summary
Use Firebug for testing and debugging
Bookmark
B k
k references
f
http://www.w3schools.com/js/
Embedding in browser
<script src="blah.js" type="test/javascript"></script>
Use XHTML or pseudo-HTML 5 syntax
Basic
B i JavaScript
J
S i t syntax
t
Declare local variables with var. No type declarations.
Java
Loops and conditionals similar to Java.
JavaScript arrays
38
Questions?
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.