Cs 952 Database and Web Systems Development: Lecture 6: Javascript
Cs 952 Database and Web Systems Development: Lecture 6: Javascript
Systems Development
Lecture 6: JavaScript
Structure of the course
2
Business 3-tier architecture
HTML
CSS
Javascript
PHP
MySQL
3
JavaScript Overview (1)
• JavaScript can be used to make HTML pages more dynamic
– It can access and manipulate Web page components
• JavaScript supports graphic effects and helps toward intelligent Web
pages
• Intelligent Web pages can verify input, perform calculations, and
make presentation decisions based upon the results
• JavaScript is a light weight programming language and is embedded
directly into the HTML code
• JavaScript was influenced by many languages, especially Java
4
JavaScript Overview (2)
• JavaScript can read and write HTML elements
– It can be used to change HTML content, HTML attributes and
HTML styles
• You can use JavaScript to detect the visitor’s browser
type and load the appropriate page for that browser
• Cookies can be created using JavaScript
• Other features such as user validation and
authentication can also be handled through JavaScript
5
Document components
• HTML tags create document objects
• JavaScript lets you manipulate those objects
• For example, you use the HTML <body>
and </body> tags to create a Web page, or
document
• Once that document is created, you can
interact with it by using JavaScript which will
be interpreted by the user’s browser
6
HTML Document Object Model (DOM)
7
The DOM Tree
• The DOM presents an HTML document as
a tree-structure (a node tree), with
elements, attributes, and text
Some of the available elements
are document specific
8
Embedding JavaScript
• There are two ways to embed JavaScript code in an
HTML file
• Use the <script> and </script> tags to place
JavaScript code directly into an HTML file
– Either in the <head> or the <body> sections or both
• Use the <script> and </script> tags to include a
separate external JavaScript file within the HTML
– The external file will contain only JavaScript statements and
have a .js extension
9
‘Internal’
<html>
<head>
<script >
// JavaScript statements go here
function processOrder() {
// more JavaScript statements go here
}
</script>
</head>
<body>
<form name="myForm">
<input type="button" name="process" value="Click here to process your order"
onClick="processOrder();">
…
</form>
</body>
</html>
10
‘External’
<head>
<script src="myscript.js">
</script>
</head>
<body>
<a href="javascript:processOrder();">Click
here to process your order.</a>
...
</body>
</html>
11
JavaScript
• The syntax and language is similar to PHP
– Variables (dynamically typed)
• Number, string, Boolean, null, arrays, function, objects
– Operators
• Arithmetic, assignment, comparison, logical, conditional
– Conditions and loops
• if ... else if ... else and switch
• for and for/in loops
• while and do … while loops
String Operators
• + or += can be used to concatenate strings
txt1 = "What a very";
txt2 = "nice day";
txt3 = txt1 + " " + txt2;
15
Getting the time (2)
• Store current date in a variable:
var now = new Date();
• The date is stored as the number of
milliseconds since January 1st 1970
• We need to convert this to a useful form:
localtime = now.toString();
utctime = now.toGMTString();
16
Displaying the time
17
18
19
Event handlers
• Event handlers are built-in scripts that respond
to specific browser events
– e.g. mouse clicks, mouseover, on page load, etc.
– onabort, onblur, onchange, onclick, ondblclick,
ondragdrop, onerror, onfocus, onkeydown,
onkeypress, onkeyup, onload, onmousedown,
onmousemove, onmouseout, onmouseover,
onmouseup, onreset, onselect, onsubmit, onunload
20
Event handler - HTML file
21
Event handler – Browser display
Once OK is clicked,
the URL is loaded
22
Client-Side Processing of HTML Forms
24
Submission Example 2
25
Email Validation
<body>
<form name="getemail"; onsubmit="return check(this)";>
<p>Enter your e-mail address:
<input name="myemail" type="text"/></p>
<p><input type="reset" value="Reset">
<input type="submit" value="Submit"></p>
</form>
<script>
function check(theform) {
var myform = document.getemail;
var test = myform.myemail.value;
if (!testEmail(test)) {
window.alert(Please enter your email.);
return false; }
else { return true; }
}
function testEmail (chkMail) {
var emailPattern = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
var regex = new RegExp(emailPattern);
return regex.test(chkMail);
}
</script>
</body>
28
And LOTS
• You can play with:
more….
– Document appearance and content
• Dynamically create & modify HTML content
• Through DHTML/DOM
– Browser control
• Objects represent browser window
– HTML form interaction
• Objects represent browser contents
– User interaction
• Event handling
– Client state read/write
• Via cookies
– Applet interaction
29
Client-Side Validation
• HTML5
– Input types
• Email, url, number, password, date
– Input attributes
• Max, min, required, pattern
– Doesn’t work in all browsers
• JavaScript
– More complex checking
– More integrated error notifications
– But can be turned off
30
References
• http://www.w3schools.com/js/default.asp
Comments and Spacing
• Standard comment syntax
– Within a line
// comment here
– Block comment
/* start comment
…
end comment */
• Whitespace is ignored in JavaScript
– Spaces, tabs and blank lines do not affect the code
interpretation
Variables