HTML
HTML
Program:
Program compile Output
Script :
HTML Browser Output
•Browser: is a piece of software that enables you to view pages on the
internet.
•Every html element begin with a start tag and end with an end tag
<body>
<p> Body of the document </p>
</body>
</html>
HTML Tag Types
•HTML tags may tell a WWW browser to:
–format the text (bold, italicize, etc.)
–have an in-line image
•make it into a header
•The ending part of the tag, </tag name>, contains the "/" slash character.
•This "/" slash tells a WWW browser to stop tagging the text. Many HTML
tags are paired this way. Think of the tag as a container.
•If you forget the backslash, a WWW browser will continue the tag for the
rest of the text in your document,producing undesirable results.
Steps to Creating a HTML Document
•Open a text editor
•Display your HTML document in WWW browser window (Open File rather
than Open Location)
•<ul>Unordered List
– <li>first item
– <li>second item
•</ul>
Common Tags Used
•<HTML> </HTML> Opens and closes the HTML document.
•<title> </title> This is the title of your page and will appear at the top of
your browser’s window.
•<h1> </h1> to <h5> </h5> Header sizes for titles and larger text
formats. Or specify font size: <font size=“4”> </font>
Common Tags Used
•<img src=“filename.gif”>
–Sources an image file and needs no closing tag.
•<a href=“filename.html”> Place URL </a>
– Links text or images to the designated file.
•<b> text </b> Bold text.
EXAMPLE :-
<DL>
<LH>List Header</LH>
<DT>Term 1<dd>This is the definition of the first term
<DT>Term 2<dd>This is the definition of the second term.
</DL>
which could be rendered as:
List Header
Term 1
This is the definition of the first term.
Term 2
This is the definition of the second term.
table
<table>
<thead>
<th> </th> //represents heading content
</thead>
<tbody>
<tr> </tr> //represents row content
</tbody>
</table>
Data in table is represented by using <td> tag
<table border=1>
<thead>
<tr> <th> reg no</th> <th> name</th> </tr>
</thead>
<tbody>
<tr>
<td> 1</td>
<td> a </td>
</tr>
<tr>
<td> 2</td>
<td> b </td>
</tr>
</tr>
</tbody>
</table>
<table border=1>
<tr>
<td> 1</td>
<td> a </td>
</tr>
<tr>
<td> 2</td>
<td> b </td>
</tr>
</tr>
</table>
</body>
</html>
Some more attributes
<colgroup> specifies the format of the group of columns
Ex:-
<colgroup> //specified before the <thead> tag
<col align=“right”>
<col span=“3” align=“center” valign=“top” width=“20”>
</colgroup>
The first <col> specifies the alignment for the first column in the table
The second <col> specifies the alignment for the span of 4 colums in the table
<align> specifies the data alignment in the table
<valign> specifies the vertical alignment (top, bottom, middle) of the data. It
if effective when the rowspan of the col is more than 1.
<html>
<head> <title> table example using colgroup </title>
</head>
<body>
<table border=1>
<colgroup>
<col align="right">
<col span=4 align="center" width=10 height=200 valign="top">
</colgroup>
<thead>
<tr><td>reg</td><td>name</td><td>branch</td><td>%</
td><td>credits</td></tr>
</thead>
<tbody>
<tr>
<td>1</td><td>a</td><td>cse</td><td>80</td><td>56</td>
</tr>
</tbody>
</body>
</html>
Example Program
FRAMES
•Frame allows the user to view multiple html documents at a time.
Syntax:-
<frameset cols=“10%,90%”>
<frame name=“f1” src=“./file1.html”>
<frame name=“f2” src=“./file2.html”>
</frameset>
Note:- <frameset> appear after the <head> of the html document
Nested frame
<frameset cols=“10%,90%”>
<frame name=“f1” src=“./file1.html”>
<frameset rows=“30%,50%,20%”>
<frame name=“f21” src=“./file2.html”>
<frameset cols=“20%,40%,40%”>
<frame name=“f221” src=“./file2.html”>
<frame name=“f222” src=“./file3.html”>
<frame name=“f223” src=“./file4.html”>
</frameset>
<frame name=“f23” src=“./file3.html”>
</frameset>
</frameset>
Syntax:-
•<meta name=“keywords” content=“html, introduction, tags, table, frame”>
-- Diplay the list of key aspects of the site. It gives precise information about
the site to the search engine.
•<meta name=“description” content=“this site gives the basic information
about the html tags”>
Method indicates the way the web server organize and send you the output.
“Post” causes changes to server data, ex:- updating a database
“get” is used when the form does not cause any changes to the server
database. Ex:- requesting database
<input> tag is used to create appropriate input elements on the form
Ex:- textbox, textarea, buttons etc..
<style type=“text/css”>
p { color:red; font-family: arial }
•Using external style sheets ( external style)
A { text-decoration : overline; }
Li em { color : red;
font-weight : bold;
}
Ul { margin-left : 2em ; }
Synatx :
Note : the <link> tag appear in the <head> of the html document
An example
<html>
<head>
<link rel = “stylesheet” type=“text/css” href=“example.css”>
</head>
<body>
<a href=“example.html” > sitams </a>
</body>
</html>
more style attributes
p, h1 { color: yellow;
text-decoration: blink;
font-weight: 10pt;
font-size: 12px;
}
Here <h1> is the child element of <p>.
So this style is applicable all <h1> children of <p>
Font-size
•Font-size is represented in two ways
1.Relative-length measurement
•color : red/green/yellow
•Background-repeat : no-repeat
•Background-attachment : fixed
Text
•Text-indent : 1em
•Text-align : center/left/right/justify
Margin
•Width : 20%
•Height : 10%
•Overflow : scroll
•Margin-bottom/margin-left/margin-right/margin-top
Padding-top/padding-bottom/padding-left/padding-right
Ex:- padding-left:0.5em
example
Div
{ background-color : red;
margin-bottom : 0.5em
font-size: 1.5em
width : 50%
overflow: scroll;
padding-left : 0.5em
}
•The <div> tag defines a division or a section in an HTML document.
•The <div> tag is often used to group block-elements to format them with
style
•Case-sensitive
•General purpose prog. Lang.
•Uses applets to interact with web applications
•Product of sun-micro systems
•Java has both Compiler/interpreter
JavaScript
•Object-based lang
•Does not support inheritance and polymorphism
•Variables are treated as Strings only
•Case-sensitive
•client-side scripting lang.
•Uses html to interact with web applications
•Product of Netscape
•Javascript is a interpreter
JavaScript introduction
•The code is written in the <script> tag of the <html> document.
•JavaScript treat the elements of the web-page as objects.
•Objects in javascript
Ex:- document.writeln(“hello”);
•HTML tags can be used within the “ “ to invoke the functionality of the tag
Ex:-
document.writeln(“<h1 style=“color:red”>hello</h1>”);
•variables are declared by using “var”
{
statement1;
statement2;
}
•While(condition)
{
statement1;
statement2;
}
•do
{
statement1;
statement2;
}
while(condition);
Control structures
•if(condition)
statement;
•if(condition)
statement1;
else If(condition)
statement2;
Example for average
<html>
<head>
<script language=“javascript”>
var total,count,sum,avg,n;
count=1; total=0;
while(count <10)
{ n=window.prompt(“enter n”,0);
total=total+parseInt(n);
count=count+1;
}
avg=total/count;
document.writeln(“average of n is:”+avg);
</script>
</head>
<body>
Click to get average of n numbers </p>
</body>
</html>
Switch-case
Switch(choice)
{
case option1 : statement1;
break;
case option2 : statement2;
break;
case option3 : statement3;
break;
default : statement 4;
break;
}
•Arithmetic operators
+,-,*,/,%,++,--
•Logical operators
&&, || , !
•Relational operators
<,<=,>,>=,==,!=
Squares of the numbers
<html>
<head>
<script language="javascript">
document.writeln("Square of numbers between 1 to 10");
for(var i=0;i<=10;i++)
{
document.writeln(" the square of "+i+" is : "+square(i));
}
function square(j)
{
return j*j;
}
</script>
</head>
<body>
</body>
</html>
Functions()
Syantax:-
function function-name(parameters list)
{
declarations;
statement1;
statement2;
}
Ex:- function fun(x,y,z)
{ return x+y+z;
}
Note:- function either return value/ does not return value
Square of a numbers
<html>
<head>
<script language=“javascript”>
var a;
for(var i=1;i<=10;i++)
document.writeln(“the square of “+i+” is : “+square(i)):
function square(x)
{
return x*x;
}
</script>
</head>
<body></body>
</html>
Example:-
<html>
<head>
<script language="javascript">
for(var i=0;i<10;i++)
{
var x=Math.floor(1+Math.random()*6); //generates number between 1 to 6
document.writeln("the random nummber is: "+x);
}
</script>
</head>
<body> </body>
</html>
recursion
•Function which calls by itself either directly or indirectly.
•Program to generate the factorial of series of numbers
<html>
<head>
<script language=“javascript”>
for(var i=0;i<=10;i++)
document.writeln(“<br>Factorial of “+i+” is: “+factorial(i));
function factorial(x)
{
if( x<=1) return 1;
else return (x * factorial(x-1));
}
</script>
</head>
<body></body>
</html>
Arrays
•An array is a group of memory locations that all have the same name and are
of same type.
•Array commences from zero index.
•The length of the array is obtained using length method
Ex:- array_name.length
•Declaration of one dimensional array
var a=[10,20,30,40,50];
(or)
var a=new Array(10,20,30,40,50);
ex:-
var a=new Array(10);
sort(a); //where sort is a user defined function
Exercise:-
•Program to sort numbers using function
•Program to search for an element in an array
Multi-dimensional Arrays
•Declaration of a two-dimensional array
Ex:- var a;
a=new Array(2); //allocate 2 rows
a[0]=new Array(3); // 3 elements in 1st row
a[1]=new Array(4); // 4 elemets in 2nd row
•Initialization
for (var i in a)
for( var j in a[i] ) document.writeln( a[i][j] );
(or)
for( var i=0; i<a.length; i++) //to access rows
for(var j=0;j<a[i].length;j++) // to access elements in each row
document.writeln( a[i][j] );
Exercise :-
Program to perform matrix multiplication
JavaScript : Objects Math Object
•Math.random() generates number between 0 and up to 1 but not including 1.
•Math.floor()
•Math.ceil()
•Math.square()
•Math.max(x,y)
•Math.min(x,y)
•Math.abs(x)
•Math.pow(x,y) // xy
•Math.round(x)
•Math.log(x);
•Math.sin(x), Math.cos(x), Math.tan(x)
•Math.sqrt(x)
•Math.exp(x) // ex
•Math.random()
String Object
•The String object encapsulates the attributes and behavior of a string of
characters.
•The string object provides many methods for selecting characters from a
string, combination of strings, obtaining substring of a string, searching for
substring within a string, tokenizing strings, conversion to lowercase or
uppercase etc…
Methods
•Let String s=“I am a javascript learner”;
•charAt(index) – returns a string containing the character at the specified
index. It returns null if there is no character at the specified index.
•charCodeAt( index) – Returns Unicode value of the character at the specified
index. If there is no character at the index, charCodeAt returns NaN (Not a
Number).
•Concat(string) – Concatenates its argument to end of the string that invokes
the method.
•fromCharCode(value1,value2 …) – converts a list of Unicode values into a
string containing the corresponding characters.
•indexOf(substring) – method returns the position of the first occurrence of a
specified substring starting from the begining
•indexOf(substring,index) – method returns the position of the first occurrence
of a specified substring starting from position index towards the beginning
•lastIndexOf(substring) – searches for the last occurrence of substring The
method returns the starting index of substring in the source string or -1 if
substring is not found.
•lastIndexOf(substring, index) – searches for the last occurrence of substring
starting from position index and searching toward the beginning of the string
that invokes the method. The method returns the starting index of substring in
the source string or -1 if substring is not found.
•Slice(start,end) – returns a string containing the portion of the string from
index start through index end. If end index is not specified, it go up to end of
the string.
•Split(string) – splits the source string into an array of strings (tokens) where its
string argument specifies the delimiter.
•Substr(start,length) – returns a string containing length characters starting
from index start in the source string. It goes up to the end, if the length is not
specified.
•toLowerCase() – returns a string in which all uppercase letters are converted
to lowercase letters.
•toUpperCase() – returns a string in which all lowercase letters are converted
to uppercase letters.
•toString() – returns same string as the source string.
•valueOf() – returns the same string as the source string.
•isNaN() – returns true if the value is not a number
function validate()
{
var un=login.ln.value;
var pwd=login.pwd.value;
var c=un.charAt(0);
if( un!=" " && pwd!=" ")
{
if( c >= 'a' && c<='z' || c>='A' && c<='Z' )
{
if(pwd.length > 6)
{
//document.login.submit();
//alert("succ");
return true;
}
else
{
alert ("password must be atleast 6 chars");
return false;
}
}
else if (un!=" ")
{
alert (" username firt character must be a letter");
return false;
}
}
contd…
else
{
alert ("enter all the fields");
return false;
}
}
</script>
</head>
<body>
<form name="login“ action=“./ex1.html” onsubmit=“return validate()”>
Login: <input type=text name=ln><br>
Password: <input type=password name=pwd>
<input type=submit value="submit" onclick="validate()">
</form>
</body>
</html>
Note :- on successful validation, the control goes to ex1.html
excercise
Registration form validation
•Validate phone number ( must be 10 digits)
•Validate email id
sitams@yahoo.com
Check for the position of @ ( it should be >0)
Check for the position of . (it should be > pos(@)+1)
The length of email_id > pos(.)
•Validate credit card number ( 16 digit number)
User Profile validation
•Date of Birth
•Validate age (numeric between 1-100)
•Alternate email_id
Usage:-
var d=new Date();
d.getTime();
example
<html>
<head>
<script language="javascript">
var current=new Date();
document.writeln("toString: "+current.toString()+"<br>");
document.writeln("toLocalString: "+current.toLocalString()+"<br>");
document.writeln("toUTCString: "+current.toUTCString()+"<br>");
document.writeln("tovalueOf: "+current.valueOf()+"<br>");
document.writeln("getDate: "+current.getDate()+"<br>");
document.writeln("getMonth: "+current.getMonth()+"<br>");
document.writeln("getFullYear: "+current.getFullYear()+"<br>");
document.writeln("getHours: "+current.getHours()+"<br>");
document.writeln("getMinutes: "+current.getMinutes()+"<br>");
document.writeln("getSeconds: "+current.getSeconds()+"<br>");
document.writeln("getMilliseconds: "+current.getMilliseconds()+"<br>");
document.writeln("getTimezoneOffset: "+current.getTimezoneOffset()
+"<br>");
</script>
</head> <body> </body>
</html>
Dynamic HTML
(DHTML)
Event Model
•onload – uses this event to call javascript function when document loading
completes.
function load()
{
window.alert("the document is loaded");
}
</script>
</head>
<body onload="load()">
<h3> the demo of onload event </h3>
</body>
</html>
<html>
<head>
<script language="javascript">
function validate()
{
alert("validation successful");
function bye()
{
alert("the form is cleared");
}
</script>
</head>
<body>
<form>
username : <input type="text" name="un"> <br>
password :<input type="password" name="pwd" onfocus=alert("pwd must be
6 chars") onblur=alert("bye") > <br>
•event.offsetY
•event.srcElement
•event.srcElement.tagName
•elementname.innerText
function change()
{
event.srcElement.style.color="RED";
}
function nochange()
{
event.srcElement.style.color="black";
}
</script>
</head>
<body onmouseover="change()" onmouseout="nochange()">
<span id="coordinates">(0,0)</span>
<img src="./picture.gif" width=40 height=60>
<h1> SITAMS college </h1>
<ul>
<li> CSE
<li>IT
</ul>
</body>
</html>
DHTML: Filters & Transitions
Filters & Transitions
•Many visual effects are implemented directly in the client-side browser
•Filters and transitions do not add content to the web page, rather they present existing content in an engaging
manner to capture the user’s attention.
•Applying filters to text or an image causes changes that are persistent (permanent)
•Transitions are temporary; applying a transition allows you to transfer from one page to another page with a
pleasant visual effect ex:- random dissolve
•Filters and transitions are applied to the web page elements using CSS filter property.
filters
•Flip : flip filters are applied to the text
•Types : flipv and fliph
•Fliph – flips the text horizontally
•Flipv – flips the text vertically
•Usage :-
s.filter.item(0). enabled=true;
s.filters(“blur”).strength=10;
</script>
</head>
<body>
<span style="filter: fliph"> sitams</span>
<span id=“s”> sitams</span>
</body>
</html>
•Filter types
Mask filter
Glow filter
Blur filter
Wave filter
Invert filter
Gray filter
Xray filter
chroma filter