Inte 221 Web Application
Inte 221 Web Application
KABARAK UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE
INTE 221: Web Application Programming
Instructor : Mr Masese
Course objectives
Ø Explain introduction to internet and ways to access information form world wide
web
Ø Create dynamic web pages by hand-coding HTML
Ø Find Creating simple web based applications using HTML
Ø Validating a web page using java script
Course Outline.
WEEK 1-2 Introduction Internet
WEEK 10-11 Data types and functions in java script , Event handling and form
References:
1. HTML for the World Wide Web - 6th Edition by Elizabeth Castro PeachPit Press,
ISBN 0321430840, 2006
2. Web Technologies: A Computer Science Perspective, Jeffrey C. Jackson, 2007,
Pearson Prentice Hall
3. Eric Meyer On CSS and More Eric Meyer On CSS by Eric A. Meyer New Riders,
ISBN 0-7357-1425-8, 2004
4. Transcending CSS, The fine art of web design by Andy Clarke New Riders, ISBN:
0-321-41097-1, 2007
5. Essential ActionScript 3.0 by Colin Moock O'Reilly, ISBN 978-0-596-52694-8
6. PPK on JavaScript By Peter-Paul Koch New Riders, ISBN: 978-0-321-42330-6,
2007
Prepared by Masese N
2
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
INTERNET
The Internet is a collection of information stored in computers that are networked together
internationally. It is literally a network of networks. Physically, the Internet uses a subset of
all of the resources of the currently existing public telecommunication networks.
Technically, what distinguishes the Internet as a cooperative public network is its use of a
set of protocols called TCP/IP (Transmission Control Protocol/Internet Protocol).
How Do I Connect to the Internet?
∙ Computer
∙ Connection - Phone Line, Cable, DSL, Wireless, ...
∙ Modem
∙ Network Software - TCP/IP
∙ Application Software - Web Browser, Email, ...
∙ Internet Service Provider (ISP)
∙ Dial-up access uses a modem and a phone call placed over the public
switched telephone network (PSTN) to connect to a pool of modems
operated by an ISP.
The modem converts a computer's digital signal into an analog signal that
travels over a phone line's local loop until it reaches a telephone company's
switching facilities or central office (CO) where it is switched to another phone
line that connects to another modem at the remote end of the connection
Prepared by Masese N
3
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
The World Wide Web (or WWW) is a subset of the Internet. Technically it is all the resources
and users on the Internet that are using the Hypertext Transport Protocol (HTTP).
Prepared by Masese N
4
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
BROWSER
A browser program enables your computer to extract information from remote computers
working on any platform through the WWW. The program on your computer is called WWW
client. The WWW program [usually] on another computer that the browser "talks" to is called
the server
Technically, a Web browser is a client program that uses the Hypertext Transport Control
Prepared by Masese N
5
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
Protocol (HTTP) to make requests of Web servers throughout the Internet on behalf of the
browser user
Web browsers communicate with Web servers primarily using HTTP (Hypertext Transfer Protocol) to
fetch Web pages. HTTP allows Web browsers to submit information to Web servers as well as fetch
Web pages from them.
Internet Explorer
Windows Internet Explorer (formerly Microsoft Internet Explorer abbreviated as MSIE), commonly
abbreviated as IE, is a series of graphical web browsers developed by Microsoft and included as part
of the Microsoft Windows line of operating systems starting in 1995. It has been the most widely
used web browser since 1999.
Internet Explorer has been designed to view the broadest range of web pages and to provide certain
features within the operating system including Microsoft Update. Some of its features are as follows:
1. Standard support
Internet Explorer almost fully support HTML, CSS XML implementation techniques.
4. Security
Internet Explorer uses a zone based security framework that groups sites based on certain
Prepared by Masese N
6
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
conditions, including whether it is an intranet or internet based site. Security restrictions are
applied on a per zone basis, all the sites in a zone are subject to the restrictions.
5. Group Policy
Internet Explorer is fully configurable using Group Policy (feature that provides centralized
management and configuration of computers and remote users). Administrators of Windows
Server Domains (a logical group of computers running versions of the Microsoft Windows
operating system that share a central directory database) can apply and enforce a variety of
settings that affect the user interface (such as disabling menu items and individual
configuration options), as well as underlying security features such as downloading of files,
zone configuration, per site settings, etc.
Netscape Navigator
Netscape Navigator, also known as Netscape, is a proprietary (proprietary software is a term for
computer software with restrictions on use or private modification, or with restrictions judged to be
excessive on copying or publishing of modified or unmodified versions) web browser that was
popular during the 1990s. It is a closed source , non free web browser.
3) Browsers can connect with any Web server, no matter what operating system or
platform.
4) Browsers require few system resources and little if any maintenance and system
configuration.
5) Browsers can easily be updated by downloading the latest version from the Web
page of the browser’s developer or vendor.
URL
A URL (Uniform Resource Locator) (pronounced "you-are-EL" or, in some quarters, "earl") is
the address of a file or other resource accessible on the Internet
Prepared by Masese N
7
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
∙ URL structure
protocol://host.domain[:port]/path/file.extension [?optional stuff]
http://www.hellohelpme.com/index.html
http: protocol
//www. host
hellohelpme.com domain
/index.html the individual document
the extension html shows that it was coded with html (web documents frequently have the
extension html or htm)
∙ Protocol might be - http, file, gopher, wais, news, telnet, https, ....
∙ The CASE of URLs may be important! Use only lower case in URLs that you build!
∙ "optional stuff" - after a ? - is usually search parameters or values for variables. Often
used to pass parameters to search engines, databases, or CGI scripts.
HTTP
Prepared by Masese N
8
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
The Hypertext Transfer Protocol (HTTP) is the set of rules for exchanging files (text,
graphic images, sound, video, and other multimedia files) on the World Wide Web. Relative
to the TCP/IP suite of protocols (which are the basis for information exchange on the
Internet), HTTP is an application protocol.
HTTP include the idea that files can contain references to other files whose
selection will elicit additional transfer requests. Any Web server contains, in addition to the
Web files it can serve,
Your Web browser is an HTTP client, sending requests to servers. When the browser
user enters file requests by either "opening" a Web file (typing in a Uniform Resource Locator
or URL) or clicking on a hypertext link, the browser builds an HTTP request and sends it to
the Internet Protocol address indicated by the URL
HYPERTEXT
In a hypertext document, certain words within the text are marked as links to other areas of
the current document or to other documents
The user moves to a related area by moving his or her mouse pointer to the link and clicking
once with the mouse button
Links are used to point to another part of the same document, in which case clicking the link
will cause the browser to move to a new part of the currently displayed document
HYPERMEDIA
hypermedia links uses the images tom point to HTML documents. For instance, a
hypermedia link might point to an audio file, a QuickTime movie file, or a graphic file such as
a GIF- or JPEG-format graphic
HEAD
The HEAD tag defines an HTML document header. The header
contains information about the document rather than information to
be displayed in the document. The web browser displays none of the
information in the header, except for text contained by the TITLE tag.
Syntax
<HEAD>...</HEAD>
Example
<HEAD>
<TITLE> Mozilla speaks out</TITLE>
<BASE HREF="http://www.mozilla.com">
</HEAD>
TITLE
The title element contains your document title and identifies its
content in a global context. The title is typically displayed in the title
bar at the top of the browser window, but not inside the window
itself.
The title is also what is displayed on someone's hot list or bookmark
list, so choose something descriptive, unique, and relatively short. A
title is also used to identify your page for search engines
BODY
The second--and largest--part of your HTML document is the body,
which contains the content of your document (displayed within the
text area of your browser window). The tags explained below are
used within the body of your HTML document.
For Example:--
<html>
<head> // basic document structure
<title>
MY WEB
</title>
</head>
<body>
HTML Tutor Lesson 1
</body>
</html>
Prepared by Masese N
1
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
Tags Meaning
<B> SAMPLE </B> Bold text
<I>SAMPLE </I> Italic text
<U> SAMPLE </U> Underlined text
<H1><I>Welcome Home!</I></H1>
<B><I>This is bold and italic</I></B>
ORDERED LIST:
<OL> <UL>
<LI> Item number one. <LI> First item.
<LI> Item number two. <LI> Second item.
<LI> Item number three. <LI> Third Item.
</OL> </UL>
IMAGE TAG
UNORDERED LIST:
To add graphics, you use an empty tag called the <IMG> (image) tag, which you insert into
the body section of your HTML document as follows:
<HR>
<P>This is a test of the Image tag. Here is the image I want to
display:</P>
<IMG SRC="image1.gif">
Prepared by Masese N
1
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
<HR>
The ALT attribute for the <IMG> tag is designed to accept text that describes the graphic, in
case a particular browser can't display the graphic
The basic link for creating hypertext and hypermedia links is the <A>, or anchor, tag. This tag
is a container, which requires an </A> to signal the end of the text, images, and HTML tags
that are to be considered to be part of the hypertext link. Here's the basic format for a text
link:
Prepared by Masese N
1
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
Prepared by Masese N
1
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
HTML FORMS
GET
This is the default action. If a URL is specified as the value of the action, a ? followed by the
input is sent to the server or whatever processing agent is specified.
POST
In this case the input is embedded in the form and sent to the processing agent
<form method= "get" action = "second.html">
This method is not save as it displays all the values on the address bar where by the
passwords can be easily be seen
FORM ELEMENTS
Text box : Defines a one-line input field that a user can enter text into
Radio button: let a user select ONLY ONE of a limited number of choices
Checkboxes: let a user select ZERO or MORE options of a limited number of choices.
Select option: the user selects on one option at a time
Text area : accepts rows and columns and allows the user to scroll
A submit button is used to send form data to a server. The data is sent to the page specified
in the form's action attribute
Example
<Html><head> <title> my form</title></head>
<body>
<form>
Prepared by Masese N
1
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
First Name:
Last Name:
Password :
4.Gender <input type="radio" name=" Gender " value="male" CHECKED /> Male<br />
<input type="radio" name=" Gender " value="female" /> Female
bike
car
6. text area : <textarea rows="10" cols="30"> The cat was playing in the garden.
</textarea><br />
Enter Your Comments:<BR> <TEXTAREA wrap="virtual" name="Comments" rows=3
cols=20 MAXLENGTH=100></TEXTAREA><BR>
7. select option
<select name="course">
<option value="MSC ">MSC </option>
Prepared by Masese N
1
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
<option value="MBA">MBA</option>
<option value="BA">BA</option>
<option value="ENG">ENG</option>
</select><br />
Select an option:
</form>
</body>
</html>
Prepared by Masese N
1
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
Prepared by Masese N
1
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
IMAGE TAG
Image tag
<IMG SRC="Earth.gif" WIDTH="41" HEIGHT="41" BORDER="0" ALT="a sentence about your
site">
Text area
TEXTAREA wrap="virtual" name="Comments" rows=3 cols=20
MAXLENGTH=100></TEXTAREA>
Selected option
Select an option:
<SELECT>
<OPTION >option 1
<OPTION SELECTED>option 2
<OPTION>option 3
<OPTION>option 4
<OPTION>option 5
<OPTION>option 6
</SELECT>
Radio button
Check box
Select an option:<BR>
<INPUT type="checkbox" name="selection"> Selection 1
<INPUT type="checkbox" name="selection" CHECKED> Selection 2
<INPUT type="checkbox" name="selection">
TABLES
The table is divided into three main parts, a header, a body and a footer. There is also an
optional caption that can be added to the table.
Table is divided into rows each having a number of columns. As with most word processing
systems it is possible to give groups of columns special features and to merge two or more
columns together or two or more rows together.
Prepared by Masese N
1
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
Element Description:--
defines a table in HTML. If the BORDER attribute is present, your browser displays the table
with a border.
<CAPTION> ... </CAPTION>
defines the caption for the title of the table. The default position of the title is centered at
the top of the table. The attribute ALIGN=BOTTOM can be used to position the caption
below the table.
<TR> ... </TR> specifies a table row within a table. You may define default attributes for the
entire row: ALIGN (LEFT, CENTER, RIGHT) and/or VALIGN (TOP, MIDDLE, BOTTOM). See
Table Attributes at the end of this table for more information.
<TH> ... </TH> defines a table header cell. By default the text in this cell is bold and
centered. Table header cells may contain other attributes to determine the characteristics of
the cell and/or its contents. See Table Attributes at the end of this table for more
information.
<TD> ... </TD> defines a table data cell. By default the text in this cell is aligned left and
centered vertically. Table data cells may contain other attributes to determine the
characteristics of the cell and/or its contents
Borders These are the lines that surround the table and each cell.
CELLSPACING. The CELLSPACING attribute tells the browser how much space to include
between the walls of the table and between individual cells. (Value is a number in pixels.)
CELLPADDING. The CELLPADDING attribute tells the browser how much space to give data
elements away from the walls of the cell. (Value is a number in pixels
Attribute Description :--
ALIGN (LEFT, CENTER, RIGHT) Horizontal alignment of a cell.
VALIGN (TOP, MIDDLE, BOTTOM) Vertical alignment of a cell.
COLSPAN=n The number (n) of rowa cell spans.
ROWSPAN=n The number (n) of rcolumn a cell spans
The general format of a table looks like this:--
<TABLE>
<!-- start of table definition -->
<TR>
<!-- start of header row definition -->
<TH> first header cell contents </TH>
<TH> last header cell contents </TH>
</TR>
Prepared by Masese N
1
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
<TR>
<!-- start of first row definition -->
<TD> first row, first cell contents </TD>
<TD> first row, last cell contents </TD>
</TR>
<!-- end of first row definition -->
<TR>
<!-- start of last row definition -->
<TD> last row, first cell contents </TD>
<TD> last row, last cell contents </TD>
</TR>
<!-- end of last row definition -->
</TABLE>
<!-- end of table definition -->
EXAMPLE OF TABLE
<html><head><title> my food</title></head><body>
<TABLE>
</TABLE></body></html>
Other attributes
∙ ALIGN. The ALIGN attribute is used to determine where the chart will appear relative
to the browser window. Valid values are ALIGN=LEFT and ALIGN=RIGHT. As an
added bonus, text will wrap around the table (if it's narrow enough) when the
ALIGN=LEFT or ALIGN=RIGHT attributes are used.
∙ WIDTH. The WIDTH attribute sets the relative or absolute width of your table in the
browser window. Values can be either percentages, as in WIDTH="50%", or absolute
values. With absolute values, you must also include a suffix that defines the units
used, as in px for pixels or in for inches (e.g., WIDTH="3.5in"). Absolute values for
table widths are discouraged, though.
∙ COLS. The COLS attribute specifies the number of columns in your table, allowing
Prepared by Masese N
2
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
∙ BORDER. The BORDER attribute defines the width of the border surrounding the
table. Default value is 1 (pixel).
∙ CELLSPACING. The CELLSPACING attribute tells the browser how much space to
include between the walls of the table and between individual cells. (Value is a
number in pixels.)
∙ CELLPADDING. The CELLPADDING attribute tells the browser how much space to
give data elements away from the walls of the cell. (Value is a number in pixels.)
<html><head> </head><body>
<audio controls>
</audio>
</body>
</html>
Ogg is an open and standardized bitstream container format designed for streaming and
manipulation.
mpeg
an international standard for encoding and compressing video images. "Moving Picture
Experts Group" or "Audio/visual file format"
Prepared by Masese N
2
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
FRAMES
HTML frames allow authors to present documents in multiple views, which may be
independent windows or subwindows. Multiple views offer designers a way to keep certain
information visible, while other views are scrolled or replaced.
For example, within the same window, one frame might display a static banner, a second a
navigation menu, and a third the main document that can be scrolled though or replaced by
navigating in the second frame.
Layout of frames
An HTML document that describes frame layout (called a frameset document) has a
different makeup than an HTML document without frames. A standard document has one
HEAD section and one BODY.
The Frameset Tag
The <frameset> tag defines how to divide the window into frames,Each frameset defines a
set of rows or columns
The values of the rows/columns indicate the amount of screen area each row/column will
occupy
TYPES OF FRAMES
Vertical frameset
<html><body>
<frameset cols="25%,50%,25%">
<frame src="frame_a.htm">
<frame src="frame_b.htm">
<frame src="frame_c.htm">
</frameset></body></html>
Prepared by Masese N
2
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
<html> <body>
<frameset rows="25%,50%,25%">
<frame src="frame_a.htm">
<frame src="frame_b.htm">
<frame src="frame_c.htm">
</frameset><body>
</html>
<frame src="frame_a.htm">
<frameset cols="25%,75%">
<frame src="frame_b.htm">
<frame src="frame_c.htm">
</frameset>
</html>
Example 2
<html>
<head>
<title> Second example of frames </title>
<frameset rows="20%, 80%">
<frame src="page1.html" name="top" marginheight="1"
scrolling="auto">
<frameset cols="25%, 75%">
<frame src="page2.html" name="left" marginheight="1" scrolling="no">
<frame src="page3.html" name="right" marginwidth="3"
marginheight="1" scrolling="auto">
</frameset>
</frameset>
Prepared by Masese N
2
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
<html>
<frameset rows="25%75%">
<frameset cols="25%,75%">
<frame noresize="noresize" src="frame_b.htm">
<frame noresize="noresize" src="frame_c.htm">
</frameset>
</frameset>
</html>
<html>
<head>
<titile></title>
</head>
<body>
<body bgcolor="wheat">
<table width="500" height="50" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><img src="d:\web design\FLOWER103.JPG"width="150" height="80"></td>
<td><img src="d:\web design\ban.JPG" width="500" height="70"></td>
<td><img src="d:\web design\white_W.gif"></td>
</tr>
</table>
</BODY>
<HTML>
SIDE FRAME
<HTML>
<HEAD>
<TITLE>MAIN PAGE</TITLE>
</HEAD>
<body bgcolor="wheat"><table>
<TR><TD><a href ="about.html"target="right frame">ABOUT...</a></td></tr>
<tr><td><BR><a href ="intro.html"target="right frame">HOME</a></td></tr><BR><BR>
<tr><td><BR><a href ="san.html"target="right frame">IN TECH</a></td></tr><BR>
Prepared by Masese N
2
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
Prepared by Masese N
2
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
STYLE SHEET
CSS stands for Cascading Style Sheets. It is a way to divide the content from the layout on
web pages.
The CSS syntax is made up of three parts: a selector, a property and a value:
The selector is normally the HTML element/tag you wish to define, the property is the
attribute you wish to change, and each property can take a value. The property and value are
separated by a colon and surrounded by curly braces:
p{ text-align: center;
color: black;
font-family: arial
}
Class selector
you can define different styles for the same type of HTML element. Say that you would like
to have two types of paragraphs in your document: one right-aligned paragraph, and one
center-aligned paragraph. Here is how you can do it with styles
Prepared by Masese N
2
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
</p>
Id selector
With the id selector you can define the same style for different HTML elements.
The style rule below will match any element that has an id attribute with a value
of "green":
h1 id="green">Some text</h1>
<p id="green">Some text</p>
Example 1
<STYLE TYPE-"type/css">
<!--
BODY {background: wheat}
A:link {color: blue}
A:visited {color: red}
H1 {font-size: 24pt; font-family: arial; color :green}
H2 {font-size: 18pt; font-family: braggadocio; color :orange}
H3 {font size:14pt; font-family: Desdemona;b color: back}
-->
</STYLE>
Example 2
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
Types of style sheets
(a) inline style sheet is mainly embedded in the html document after the head
after closing the title tag and before closing the head tag
Prepared by Masese N
2
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
</style> </head><body>
< span class="headlines">Welcome</span><br>
<div class="sublines">
This is an example page using CSS.<br>
</div>
<div class="infotext">
Example from Academic Articles.
</div> </body></html>
Example 2
<head><title>….</title>
body {
background-color: orange;
font-family: Arial, Verdana, sans-serif;
font-size: 18;
color: red;
}
table {
font-family: Arial, Verdana, sans-serif;
font-size: 18px;
color: orange;
}
<p> {
font-family: Arial, Verdana, sans-serif;
font-size: 14px;
color: purple;
font-weight: bold;
}
</head>
<body>
Prepared by Masese N
2
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
CSS can be defined for entire sites by simply writing the CSS definitions in a
plain text file that is referred to from each of the pages in the site
<html>
<head>
<title>MY CSS PAGE</title>
<link rel=stylesheet href="whatever.css" type="text/css">
</head>
<body>
<span class="headlines">Welcome</span><br>
<div class="sublines">
This is an example of a page using CSS.<br>
The example is really simple,<br>
and doesn't even look good,<br>
but it shows the technique.
</div>
<table border="2"><tr><td class="sublines">
As you can see:<br>
The styles even work on tables.
</td></tr></table>
<hr>
<div class="infotext">Example from Academic Articles.</div>
<hr>
</body>
</html>
This means that the browser will look for a file called whatever.css and insert it at the place
where the reference was found in the html document.
whatever.css
headlines, .sublines, infotext {font-face:arial; color:black; background:yellow;
font-weight:bold;}
.headlines {font-size:14pt;}
.sublines {font-size:12pt;}
.infotext {font-size: 10pt;}
Now if you just add the line <link rel=stylesheet href="whatever.css" type="text/css"> to the
<head> of all your pages, then the one style definition will be in effect for your entire site.
Prepared by Masese N
2
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
pages link to this centralised CSS file, then the look of a website can more easily be
updated.
Streamlined maintenance
As less code is required for each webpage, both the likelihood of coding errors and
time required to add content to a website are reduced.
Prepared by Masese N
3
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
Script tag
<script language = java script >
<!...
document. write( welcome to java script programming );
//…>
</script>
Where to place it
The browser will use the <script> type="text/javascript"> and </script> to tell where
javascript starts and ends.
Example 1
<html>
<head><title>My Javascript Page</title>
</head><body>
<script type="text/javascript">
alert("Welcome to JAVA PROGRAMMING!!!");
</script>
</body>
</html>
Example 2
<html>
<head>
<title>My Javascript Page</title>
<script>
document.write("Welcome to my world!!!<br>");
Prepared by Masese N
3
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
</script>
Enjoy your stay...<br>
</head>
<body>
Hello!!!<br>
</body>
</html>
The document.write is a javascript command telling the browser that what follows within
the parentheses is to be written into the document
∙ A prompt box is used when you want some input from the user.
USES JAVASCRIPT:
∙ Browser Detection
Detecting the browser used by a visitor at your page. Depending on the browser,
another page specifically designed for that browser can then be loaded.
∙ Cookies
Storing information on the visitor's computer, then retrieving this information
automatically next time the user visits your page. This technique is called "cookies".
∙ Control Browsers
Opening pages in customized windows, where you specify if the browser's buttons,
menu line, status line or whatever should be present.
∙ Validate Forms
Validating inputs to fields before submitting a form.
An example would be validating the entered email address to see if it has an @ in it,
since if not, it's not a valid address.
Prepared by Masese N
3
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
var num // declare variable, has null value( var used to represent string information)
num = 20 // assign a value, null value is replaced
//OR
var num=20 // declare AND assign value (initialize)
var a,b,c,d //multiple variables may be declared simultaneously
var a = 5, b = 6, c = 7,d=8 //multiple variables may be initialized simultaneously
Variable Scope
There are two types of Variable scope:-
1. Globle scope Variable,
2 . Local scope Variable.
1.Globle scope Variable:- A variable declared or initialized outside a function body has a
global scope, making it accessible to all other statements within the same document.
2. Local scope Variable:- A variable declared or initialized within a function body has a local
scope, making it accessible only to statements within the same function body
Prepared by Masese N
3
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
DATA TYPES
In java script data can be represented into the following major types are Number, String,
Boolean
a) PRIMITIVE DATA types are the simplest building blocks of a program. There are following
types :-
1 Number
Used to perform the arithmetic operations such as addition, subtraction, multiplication and
division, any whole number or floating point literal that does not appear between quotation
marks( ) is considered as number
Example: 67.,29,40
2.String
String represents any sequence of zero or more characters that are to be strictly text that
os no mathematical operations can be performed on them.
Examples: hallo , 200 , kabarak
3.Boolean
Is used with the logical operations and can have one of two values true or false.
4. Null:-The null value represents no value that means strings are empty .
5. Undefined:- A variables to be declared but given no any initial value then it runtime error
display
Prepared by Masese N
3
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
OPERATORS
Java script operators are symbols and key words you use to assign values to variables or
perform operations with those values ,using the operations you can combine the variables
and literals values into expression that perform calculation and produce results
Arithmetic operators
They are used to perform the arithmetic operators on numbers they are divided to unary to
binary
Unary operators change the value of a single value or expression while Binary operators
change the value of two values or expressions ,
Assignment operator is used to store the results of the arithmetic to a variable
Unary operators
++ increment example 5++
-- decrement example 6---
Binary operators
+ addition * multiplication % modulus
- subtraction / division
Arithmetic Operators
JavaScript supports the following arithmetic operators −
1 + (Addition)
2 - (Subtraction)
Prepared by Masese N
3
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
3 * (Multiplication)
4 / (Division)
5 % (Modulus)
6 ++ (Increment)
7 -- (Decrement)
<html>
<body>
<script type="text/javascript">
<!--
var a = 33;
var b = 10;
var c = "Test";
document.write("a + b = ");
Prepared by Masese N
3
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
result = a + b;
document.write(result);
document.write(linebreak);
document.write("a - b = ");
result = a - b;
document.write(result);
document.write(linebreak);
document.write("a / b = ");
result = a / b;
document.write(result);
document.write(linebreak);
document.write("a % b = ");
result = a % b;
document.write(result);
document.write(linebreak);
document.write("a + b + c = ");
result = a + b + c;
document.write(result);
document.write(linebreak);
a = ++a;
document.write("++a = ");
result = ++a;
document.write(result);
document.write(linebreak);
b = --b;
document.write("--b = ");
result = --b;
document.write(result);
document.write(linebreak);
Prepared by Masese N
3
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
//-->
</script>
</body>
</html>
RELATIONAL OPERATORS
They are primary used with selection statement because they produce a Boolean result
(either true or false)
= = equal to {
!= not equal to //false
> greater than }
>= greater than or equal to
< less than
<= less than or equal to
Example
Var var1 = 99
Var var2 = -12
If (var1 = = var2 )
{
// false
}
else if (var1 > var2 )
{
// true
}
else if (var1 < var2)
Prepared by Masese N
3
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
Logical Operators
JavaScript supports the following logical operators −
If both the operands are non-zero, then the condition becomes true.
2 || (Logical OR)
If any of the two operands are non-zero, then the condition becomes true.
Ex: (A || B) is true.
3 ! (Logical NOT)
Reverses the logical state of its operand. If a condition is true, then the Logical NOT
operator will make it false.
<html>
<body>
<script type="text/javascript">
<!--
var a = true;
var b = false;
document.write(result);
Prepared by Masese N
3
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
document.write(linebreak);
result = (a || b);
document.write(result);
document.write(linebreak);
document.write(result);
document.write(linebreak);
//-->
</script>
<p>Set the variables to different values and different operators and then try...</p>
</body>
</html>
Prepared by Masese N
4
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
Comparison Operators
JavaScript supports the following comparison operators −
1 = = (Equal)
Checks if the value of two operands are equal or not, if yes, then the condition
becomes true.
2 != (Not Equal)
Checks if the value of two operands are equal or not, if the values are not equal,
then the condition becomes true.
Ex: (A != B) is true.
Checks if the value of the left operand is greater than the value of the right operand,
if yes, then the condition becomes true.
Checks if the value of the left operand is less than the value of the right operand, if
yes, then the condition becomes true.
Checks if the value of the left operand is greater than or equal to the value of the
right operand, if yes, then the condition becomes true.
Checks if the value of the left operand is less than or equal to the value of the right
Prepared by Masese N
4
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
Prepared by Masese N
4
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
MISCELLANEOUS OPERATORS
delete used to delete an object and free up the memory
new used to create an instance of a user defined object
this used to refer to the current object
typeof return the type of an unevaluated operand
void evaluates without returning an object
+ string concatenation operator
The if Statement
The if statement is used to conditionally execute a single block of code
if statement consists of a conditional expression, known as the if test, and a block of code which is
executed if that expression evaluates to a Boolean true
SYNTAX
if (<condition >)
{
Statements
}
Example
if (browser=="MSIE") {alert("You are using MSIE")}
else {alert("You are using Netscape")};
<HTML><HEAD></HEAD>
<script language="javascript">
document.write ("checking if 5 is less than 6.......");
Prepared by Masese N
4
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
if (5 < 6) {
document.write ("<br>Condition is true : 5 is less than 6");
}
document.write ("<br>Done checking");
</script><body> <p> hi
</BODY>
</HTML>
<html> <body>
<script type="text/javascript">
//-->
</script>
</body>
</html>
Prepared by Masese N
4
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
IF ELSE
In this form, one block of code is executed if the if test passes, and a second block is executed if it
if (expression) // condition
else{
Example
<html> <body>
<script type="text/javascript">
else{
</script> <p>comment<p>
</body</html>
Prepared by Masese N
4
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
Example
<HTML>
<HEAD></head>
<script type="text/javascript">
var a = new Date()
var time = a.getHours()
if (time < 11)
{
document.write("Good morning!");
}
else{
document.write("Good day!");}
</script></BODY></HTML>
if (expression 1){
Prepared by Masese N
4
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
else{
<html>
<head>
<script type="text/javascript">
var n = prompt("Enter a number to find odd or even", "Type your number here");
n = parseInt(n);
if (isNaN(n))
{
alert("Please Enter a Number");
}
else if (n == 0)
{
alert("The number is zero");
}
else if (n%2) // else if ( = n/2 = 0)
{
alert("The number is odd");
}
else
{
alert("The number is even");
Prepared by Masese N
4
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
}
</script>
</head>
</body>
</html>
SWITCH STATEMENT
It is commonly know as case statement, switch matches an expression with a specified case and
executes the statements defined for that case.
Syntax
switch (expression)
{
case value 1:
Statement;
break;
Case value 2:
Statement;
Break;
Case value 3:
Statement;
Break
…….
……
case value n:
Statement;
Break;
default:
statement;
break;
Prepared by Masese N
4
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
<script type="text/javascript">
var n=0;
n=prompt("Enter a number between 1 to 12:")
switch(n)
{
case(n="1"):
document.write("January");
break;
case(n="2"):
document.write("Febuary");
break;
case(n="3"):
document.write("March");
break;
case(n="4"):
document.write("April");
break;
case(n="5"):
document.write("May");
break;
case(n="6"):
document.write("June");
break;
case(n="7"):
document.write("July");
break;
case(n="8"):
document.write("August");
Prepared by Masese N
4
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
break;
case(n="9"):
document.write("September");
break;
case(n="10"):
document.write("October");
break;
case(n="11"):
document.write("November");
br;
case(n="12"):
document.write("December");
break;
default:
document.write("enter value btn 1-12");
break;
}
</script> >/head> <body>
</body>
<html>
<head>
<script type="text/javascript">
var n=prompt("Enter a number between 1 and 7");
switch (n)
{
case (n="1"):
document.write("Sunday");
break;
case (n="2"):
Prepared by Masese N
5
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
document.write("Monday");
break;
case (n="3"):
document.write("Tuesday");
break;
case (n="4"):
document.write("Wednesday");
break;
case (n="5"):
document.write("Thursday");
break;
case (n="6"):
document.write("Friday");
break;
case (n="7"):
document.write("Saturday");
break;
default:
document.write("Invalid Weekday");
break;
}
</script>
</head>
</html>
LOOPING
Used to repeat the given case for specific number of times ( when the condition is true)
Prepared by Masese N
5
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
{
// Here goes the script lines you want to loop.
}
The flow chart of while loop looks as follows −
Syntax
The syntax of while loop in JavaScript is as follows −
while (expression){
Example
Try the following example to implement while loop.
<html>
<body>
<script type="text/javascript">
var count = 0;
Prepared by Masese N
5
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
count++;
document.write("Loop stopped!");
</script>
</body> </html>
<html>
<body>
<script language="Javascript">
var i=0
while (i<=10)
{
document.write("serial number" + i)
document.write("<br />")
i=i+1
}
</script></body> </html>
The do while loop
This loop will be always executed once, even if the condition is false, because the loop code are
executed before the condition is tested.
<html><body>
<script language="Javascript">
var i=40;
do
{
alert("number" + i)
i=i+1
}
while (i<=3)
Prepared by Masese N
5
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
The initial statement is executed first; commonly this statement is used to initialize a
counter variable
Test is applied to the counter variable and then the loop starts again
example
<html> <body>
<script type="text/javascript">
var count;
document.write("<br />");
document.write("Loop stopped!");
</script>
</body></html>
Prepared by Masese N
5
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
FUNCTIONS
Is a collection of one or more script that can be made to run at any point during the life of a
program.
The functions allow one to write very compact, easy to understand and reusable code by calling the
functions whenever the action is required to perform a certain task
Defining the function
<statements>
Example
var color;
If(rowNumber>2)
rowNumber =1;
else if(rowNumber = = 1)
color = red ;
else
color = white ;
rowNumber++;
Prepared by Masese N
5
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
}
example
<html>
<head>
<script language="javascript">
function showAlert()
{
var a;
var b;
a=prompt( enter value of A );
b=prompt( enter value of B );
var sum=0;
sum=a+b;
document.write(" sum= "+sum);
document.write("\t\tHello\nworld!\n");
document.write('\nWelcome to JavaScript');
}
</script>
</head>
<body>
showAlert();
</body>
</html>
Calling a function
A function waits in the wings until it is called unto the stage, you call a function by specifying its
name followed by a parenthetical list of arguments if any
Prepared by Masese N
5
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
syntax
clearPage();
…….
,…….// arguments
Example
function showmessage(){
onclick="showmessage()" >
</form> </body></html>
∙ setInterval() - executes a function, over and over again, at specified time intervals
<html> <body>
Prepared by Masese N
5
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
<script>
function myFunction()
setInterval(function(){myTimer()},1000);
function myTimer()
var t=d.toLocaleTimeString();
document.getElementById("demo").innerHTML=t;
</script>
<p id="demo"></p>
</body>
</html>
Prepared by Masese N
5
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
EVENT HANDLING
Events are actions that occur on the web page usually as a result of something the user does, for
example a button click is an event as is giving focus to a form element.
The events that occurs automatically with out the input from the user such on error, onunload
User driven they occur with the input from the user such as onlick, onkeydown
Event handler specifies which java script code to execute,often event handlers are placed within the
html tag which creates the object on which the event acts
Example
// the event handler will be triggered when the mouse passes on the link therefore you place the
event handler for a hyperlink s mouse over inside the A tag
Common events
Prepared by Masese N
5
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
Prepared by Masese N
6
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
FORM VALIDATION
JavaScript can be used to validate data in HTML forms before sending off the content to a server.
Form data that typically are checked by a JavaScript could be:
Ø has the user left required fields empty
Ø has the user entered a valid e-mail address
Ø has the user entered a valid date
Ø has the user entered text in a numeric field
Required Fields
The function below checks if a required field has been left empty. If the required field is blank, an
alert box alerts a message and the function returns false. If a value is entered, the function returns
true (means that data is OK):
Example
<Script Type='Text/Javascript'>
Function Formvalidator(){
// Make Quick References To Our Fields
Var Firstname = Document.Getelementbyid('Firstname');
// Check Each Input In The Order That It Appears In The Form!
If(Isalphabet(Firstname, "Please Enter Only Letters For Your Name"))
{
Function Notempty(Elem, Helpermsg){
If(Elem.Value.Length == 0){
Alert( enter some value ); // Helpermsg
Elem.Focus(); // Set The Focus To This Input
Return False;
}
Return True;
}
Prepared by Masese N
6
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
</Form></HTML>
Example
<Script Type='Text/Javascript'>
Function Formvalidator(){
// Make Quick References To Our Fields
Var Firstname = Document.Getelementbyid('Firstname');
Var Addr = Document.Getelementbyid('Addr');
Var Zip = Document.Getelementbyid('Zip');
Var State = Document.Getelementbyid('State');
Var Username = Document.Getelementbyid('Username');
Return True;
}
}
}
}
}
}
Return False;
If(Elem.Value.Match(Alphaexp)){
Return True;
}Else{
Alert(Helpermsg);
Elem.Focus();
Return False;
}
}
}
</Script>
Prepared by Masese N
6
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
What is PHP?
∙ PHP files can contain text, HTML, JavaScript code, and PHP code
∙ PHP code are executed on the server, and the result is returned to the
browser as plain HTML
∙ PHP can create, open, read, write, and close files on the server
Prepared by Masese N
6
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
<?php
echo "Hello World!";
Print hi ;
?>
</body>
</html>
PHP Variables
PHP variables can be used to hold values (x=5) or expressions (z=x+y).
Variable can have short names (like x and y) or more descriptive names
(age, carname, totalvolume).
Rules for PHP variables:
∙ A variable starts with the $ sign, followed by the name of the variable
∙ A variable name must begin with a letter or the underscore character
∙ A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
∙ A variable name should not contain spaces
∙ Variable names are case sensitive ($y and $Y are two different
variables)
Prepared by Masese N
6
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
$x=5;
$y=6;
$z=$x+$y;
echo $z;
?>
Prepared by Masese N
6
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
Example
<html>
<body>
</body>
</html>
welcome.php
<html>
<body>
</body>
</html>
PHP echo and print Statements
There are some differences between echo and print:
Prepared by Masese N
6
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
Create the following page, and call it basicForm2.php. This is your HTML.
Notice the ACTION attribue.
<html>
<head>
<title>A BASIC HTML FORM</title>
</head>
<body>
</FORM>
</body>
</html>
<?PHP
$username = $_POST['username'];
Prepared by Masese N
7
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
}
else {
?>
LOGIN
<body>
<?php
$msg = '';
$_SESSION['valid'] = true;
$_SESSION['timeout'] = time();
$_SESSION['username'] = 'inte221';
else
Prepared by Masese N
7
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
?>
<div class="container">
</form>
</div>
</body>
</html>
Prepared by Masese N
7
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
Logout.php
It will erase the session data.
<?php
session_start();
unset($_SESSION["username"]);
unset($_SESSION["password"]);
header('Refresh: 2; URL=login.php');
?>
<?PHP
$male_status = 'unchecked';
$female_status = 'unchecked';
if (isset($_POST['Submit1'])) {
$selected_radio = $_POST['gender'];
if ($selected_radio = = 'male') {
$male_status = 'checked';
}
else if ($selected_radio = = 'female') {
$female_status = 'checked';
Prepared by Masese N
7
Downloaded by Alex Ruto ([email protected])
lOMoARcPSD|27865993
?>
Prepared by Masese N
7
Downloaded by Alex Ruto ([email protected])