An Introduction To Web Programming
An Introduction To Web Programming
An Introduction To Web Programming
WEB PROGRAMMING
– Variable,
– Arithmetic operators,
– Assignment operator,
– Input and output,
– Arrays,
– Loops,
– Conditions,
– Modules (Functions),
– Structures, classes and objects,
– Events and event-handling
– Files and Databases
JavaScript 2
JAVA VS. JAVA-SCRIPT
• Java • Java-Script
– Java is an Object Oriented – JavaScript is a scripting
Programming (OOP) language language that was created by
created by James Gosling of the fine people at Netscape
Sun Microsystems. and was originally known as
– Java is a stand alone language LiveScript.
and runs on a virtual machine. – JavaScript must be part of a
– Java is compiled in a byte- HTML document and runs
code (an intermediate within a browser.
machine language) and – JavaScript is interpreted line-
produces a stand-alone by-line by the browser.
executable. – Object-based: Code uses built-
– Object-oriented: in, extensible objects, but no
(Encapsulation, inheritance, classes or inheritance.
polymorphism) – Dynamically typed language
– Strongly typed language.
Lean More:
http://en.wikipedia.org/wiki/JavaScript
http://sislands.com/coin70/week1/javajs.htm
JavaScript 3
WHAT IS JAVA SCRIPT?
• Java script is a scripting language which is typically used to
enhance the functionality and appearance of web pages.
JavaScript 4
JAVA SCRIPT
• The java script example Example:
creates a simple function for
displaying the date, and <!DOCTYPE html>
connects it to the event <html>
handler of a button.
<head>
<script>
function displayDate()
{
document.getElementById("placeholder").innerHTML=Date();
}
</script>
</head>
<body>
Java Script
<h1>My First JavaScript</h1>
<p id="placeholder">the date will appear here...</p>
Called when the
button is clicked
<button type="button" onclick="displayDate()">Display
Date</button>
</body>
</html>
Event
Handler
JavaScript 5
OUTPUT
• Before Click • After Click
JavaScript 6
JAVASCRIPT AND ITS RELATION WITH
DOM VS. BOM
• Document Object Model (DOM) Browser Object Model (BOM)
JavaScript 7
THE STRUCTURE OF AN HTML DOCUMENT
DOCUMENT
<html>
<head> <body>
JavaScript 8
THE STRUCTURE OF BROWSER OBJECT
Window
DOCUMENT
Detecting Array of To
Detecting
the previously
<html>
another page
>
1> > >
Other
tags
such as
<meta
>,
<link>,
Java
Etc. CSS
script
http://www.javascriptkit.com/jsref/window.shtml
JavaScript 9
AN INTRODUCTION TO
WEB PROGRAMMING
JavaScript 11
VARIABLES
• Variables in JavaScript are dynamically typed.
• Example:
JavaScript 12
VARIABLES
JavaScript 13
ARITHMETIC OPERATORS
• Same as C/C++ operators
+, -, *, /, %, ++, --
JavaScript 14
OTHER OPERATORS
• Bitwise http://www.javascriptkit.com/jsref/bitwise_operators.shtml
• Comparison
==, !=, < , >, <=, >=
http://www.javascriptkit.com/jsref/compariso
• Logical n_operators.shtml
&&, ||, !
• Other: http://www.javascriptkit.com/jsref/other_oper
typeof, new, delete ators.shtml
JavaScript 15
TYPEOF
JavaScript 16
OUTPUT
• Output methods in JavaScript apply to the DOM’s
DOCUMENT object.
• Example:
document.write(“hello”);
document.writeln(“hello”);
JavaScript 17
INPUT
• Input methods in JavaScript apply to the BOM’s
WINDOW object.
• Example:
window.prompt(msg, [input]);
var myName;
myName = window.prompt("please enter your name")
• Displays a Prompt dialog box with a message. Optional "input" argument allows you
to specify the default input (response) that gets entered into the dialog box.
JavaScript 18
INPUT
JavaScript 19
AN INTRODUCTION TO
WEB PROGRAMMING
Dynamic arrays:
var mySchedule = new Array([size]);
mySchedule[0] = “CSCI-A 340”;
mySchedule[1] = “MATH-M 215”;
Literal arrays:
var mySchedule = [“CSCI-A 340”, “MATH-M 215”];
Array properties:
mySchedule.lenght
JavaScript 21
ARRAY
JavaScript 22
OPERATION ON ARRAYS
• Array.sort([SortFunction])
• By default the function sort(), sorts the arrays alphabetically and in ascending order.
• To sort the array numerically, need to compare the relationship between "a" to "b", with a return value
of <0 indicating to sort ascending, and >0 to sort descending
http://www.javascriptkit.com/jsref/arrays.shtml#e2
JavaScript 23
OPERATIONS ON AN ARRAY:
• Sorting an Array (Bubble Sort)
End If
Next Index
Next Pass
End Sub
JavaScript 25
SORTING (NUMERIC)
JavaScript 26
OPERATION ON ARRAYS
Array. Concat()
http://www.javascriptkit.com/jsref/arrays.shtml#e2
JavaScript 27
CONCATENATION
JavaScript 28
OPERATION ON ARRAYS
Array. indexOf(targetElement)
alert(fruits.indexOf("Pork")); //alerts 2
http://www.javascriptkit.com/jsref/arrays.shtml#e2
JavaScript 29
INDEX OF
JavaScript 30
OPERATION ON ARRAYS
Array. Join([separator])
• Converts each element within the array to a string, and joins them into one large string.
Pass in an optional separator as argument to be used to separate each array element. If
none is passed, the default comma (,) is used:
Array. Map(mappingfunction())
• Returns a new array based on the return value of testfunction() on each of the array
elements. Original array is not changed. Use it to transform the values of all elements
within an array using some logic and derive the results as a new array.
Array.push(value);
• Adds the argument values to the end of the array, and modifies the original array with the
new additions. Returns the new length of the array.
Array.pop();
• Deletes the last element within array and returns the deleted element. Original array is
modified.
http://www.javascriptkit.com/jsref/arrays.shtml#e2
JavaScript 31
OTHER ARRAY OPERATIONS
JavaScript 32
ASSOCIATIVE ARRAYS
• Example:
• Associative array is an
array that uses a var testScore = []; // Create an empty array
testScore["Bob"] = 89;
“string” (instead of a testScore["Mary"] = 95;
JavaScript 33
AN INTRODUCTION TO
WEB PROGRAMMING
• Do-While Loop
var number=0;
do{
document.write(number+"<br>");
number++;
}
while (number<5) ;
• For Loop
for (var i=0; i<3; i++){
document.write("This text is repeated three times<br>");
}
JavaScript 35
WHILE LOOP
JavaScript 36
DO-WHILE LOOP
JavaScript 37
FOR LOOP
JavaScript 38
FOR-IN LOOP
JavaScript 39
ASSOCIATIVE ARRAYS AND FOR-IN LOOP
JavaScript 40
CONDITIONAL
• Same as C and C++
• If Statement
if (expression)
statement1;
else if (expression2)
statement2;
else
statement3;
• Switch Statement
switch (expression){
case label1:
statement1
break
case label2:
statement2
break
default: statement3;
}
JavaScript 41
MODULES
• JavaScript has several built-in objects. Each of these
objects have several built-in Functions:
Some Built-in Functions:
Built-in Objects: – alert();
– confirm();
– Array – prompt();
– Boolean –
–
parseInt(x);
parseFloat(x);
– Date – Math.max(x,y);
– Math.min(x,y);
– Math – Math.pow();
– Math.random();
– Number – Date();
– String –
–
toString()
charAt()
– toLowerCase()
– RegExp – toUpperCase()
http://www.tutorialspoint.com/javascript/javascript_builtin_functions.htm http://www.javascriptkit.com/jsref/math.shtml
JavaScript 42
MODULES
• User-defined Functions:
function getArea(w, h)
{
var area = w*h;
return area;
}
JavaScript 43
MODULES
• Recursive Functions:
function factorial(number)
{
if (number <=1) //base case
return 1;
else
return (number * factorial(number-1));
}
JavaScript 44
RECURSIVE FUNCTIONS
JavaScript 45
AN INTRODUCTION TO
WEB PROGRAMMING
JavaScript 47
THE STRUCTURE OF AN HTML DOCUMENT
DOCUMENT
<html>
<head> <body>
JavaScript 48
THE STRUCTURE OF BROWSER OBJECT
Window
DOCUMENT
Detecting Array of To
Detecting
the previously
<html>
another page
>
1> > >
Other
tags
such as
<meta
>,
<link>,
Java
Etc. CSS
script
http://www.javascriptkit.com/jsref/window.shtml
JavaScript 49
INTERACTING WITH BOM
• Screen: contains information about the visitor's screen.
– (height, width, colorDepth, etc)
http://www.w3schools.com/jsref/obj_window.asp
JavaScript 50
INTERACTING WITH DOM
• The Document object is the root of a document tree. It gives us access to the
document's data elements.
• Since element nodes, text nodes, attributes, comments, etc. cannot exist outside
the document, the Document object contains methods to create and access these
objects.
document.write("<b>Width of the screen (excluding the Windows Taskbar):</b> " + screen.availWidth + "<br />");
document.write("<b>Height of the screen (excluding the Windows Taskbar):</b> " + screen.availHeight + "<br />");
document.write("<b>Bit depth of the color palette for displaying images:</b> " + screen.colorDepth + "<br />");
JavaScript 52
WINDOW /BROWSER OBJECT
document.write("<b>X coordinate of the window relative to the screen:</b> " + window.screenX + "<br />");
document.write("<b>Y coordinate of the window relative to the screen:</b> " + window.screenY + "<br />");
document.write("<b>Inner Width of a window's content area:</b> " + window.innerWidth + "<br />");
document.write("<b>Inner Height of a window's content area:</b> " + window.innerHeight + "<br />");
document.write("<b>Outer width of a window, including toolbars/scrollbars:</b> " + window.outerWidth + "<br />");
document.write("<b>Outer height of a window, including toolbars/scrollbars:</b> " + window.outerHeight + "<br />");
document.write("<b>Current URL:</b> " + window.location + "<br />");
JavaScript 53
WINDOW OBJECT (CONTINUED…)
• To create a new Browser Window:
– var myWin = window.open('‘, '_blank', 'width=100, height=400'); //make sure the pop-up blocker is off
JavaScript 55
AN INTRODUCTION TO
WEB PROGRAMMING
var isMobile=navigator.userAgent.match(/(iPad)|(iPhone)|(iPod)|(android)|(webOS)/i);
document.write("<b>UserAgent is Mobile?</b> " + isMobile + "<br />");
JavaScript 57
HISTORY OBJECT
•To get the length:
•document.write("<b>Number of URLs in History object:</b> " + history.length + "<br />");
•To move Backward or Forward:
•history.back();
•history.forward() ;
function goBack()
{
window.history.go(-1);
}
function goForward()
{
window.history.go(+1);
}
<body>
<input type="button" value="Go Back" onclick="goBack()">
<input type="button" value="Go Forward" onclick="goForward()"> (Forward may not do anything here since there may be no links
in the forward history!)
</body>
JavaScript 58
LOCATION OBJECT
document.write("<b>URL of the current page:</b> " + location.href + "<br />");
document.write("<b>Show the Host:</b> " + location.host + "<br />");
document.write("<b>Show the Hostname:</b> " + location.hostname + "<br />");
document.write("<b>Show the Protocol (FILE, HTTP, HTTPS, etc:)</b> " +
location.protocol + "<br />");
document.write("<b>Replace the URL of the current page with another URL:</b> " + "<br />");
//location.replace('http://www.iusb.edu');
JavaScript 59
AN INTRODUCTION TO
WEB PROGRAMMING
<html>
<head> <body>
JavaScript 61
INTERACTING WITH DOM
• The Document object is the root of a document tree. It gives us access to the
document's data elements.
• Since element nodes, text nodes, attributes, comments, etc. cannot exist outside
the document, the Document object contains methods to create and access these
objects.
JavaScript 63
DOM OBJECT
var checkAllBtn = document.getElementById("ckallbtn");
checkAllBtn.value = "Uncheck All";
JavaScript 64
DOM OBJECT
// Get the form object
var theForm = document.getElementById("form1");
JavaScript 65
DOM OBJECT
// Get the form object
var theForm = document.getElementById("form1");
JavaScript 66
DOM OBJECT
• Two Dimensional Arrays
• Changing the internal attributes of an HTML table.
• Changing the background color, changing the format format.
JavaScript 67