1.Internet Programming-I
1.Internet Programming-I
03/16/2025
What is INTERNET?
2 The Internet is a collection of computers or networking devices connected
together which have communication between each other.
Connection of computers that done by sharing their database.
Term “Internet” means collection of interconnected networks.
Network of connected computers.
It is a cooperative effort governed by standards and rules.
It is a big TCP/IP network connecting computers all over the world which
uses TCP/IP (Transmission Control Protocol/Internet Protocol) to
communicate and that uses client/server architecture.
Has a decentralized design.
Internet made up of thousands of networks worldwide.
03/16/2025
The Internet Cont…
3
It contains three physical components
Computer (Client / Server)
Networks (communication channels)
Service
Computers on the Internet can be classified as either
Client computer:- Any computer on the network that requests services from another computer on
the network.
Server computer:- Any computer that receives requests from client computers, processes and
sends the output.
Principal activities that done on the Internet
Web
Email
FTP
Chat
03/16/2025
Remote access
4 Pros and cons of internet
Advantages
Disadvantages
sharing of data - virus treat
Enhanced communication - theft personal info
Global audience - spamming
Availability of information - pronography
Outsourcing - spread of wrong info
Online job portal - internet addiction
Social networking - information
overloading
03/16/2025
Provide entertainment
5 Basic concepts in using internet
Web page: single page of website
Website: collection web pages
Home page: first page in the website
www: collection of websites /hypertext languages or multimedia
Hyper text link: used to visit d/t pages of website
Uploading: sending information from client computer to server or
other web
Downloading: receiving information from web to our computer
Web browser: software used to access hyper text information
ISP
webserver 03/16/2025
Cont…
6
Internet Providers:
Research and Educational Institutions.
Government and Military Entities.
Businesses institute.
Private Organizations.
Commercial Providers.
Internet Protocols
Email (Simple Mail Transport Protocol).
Telnet (Login to remote host computer).
FTP (File Transfer Protocol) - transfers files between server and client.
HTTP (Hyper Text Transfer Protocol).
03/16/2025
7 protocols
03/16/2025
9
websites
A collection of publicity accessible, interlinked web pages that share a
single domian name.
Web pages, which are the building blocks of websites, are documents,
typically written in plain text interspersed with formatting instructions
of Hypertext Markup Language (HTML, XHTM).
03/16/2025
Cont…
10 Documents in the Web
The documents in the WWW can be grouped into three broad categories
based on the time the contents of the document are determined.
Static Documents:
- simple it refers to static HTML documents
Dynamic Documents:
- referred to server-side dynamic documents.
- e.g. Dynamic HTML documents,
Active Documents:
- sometimes referred as client-side dynamic documents
- e.g. Dynamic HTML documents
it is changed based on time.
03/16/2025
1.3 Web Server
11
Web sever is a program that waits for requests from the web browser.
Is the software (not a computer itself) that allows a computers to
communicate but it is common to use the word ‘Server’ to refers to the
computers.
A software program or server computer equipped to offer World Wide
Web access.
Allow internet user to serve content over Internet using (HTML).
Accepts requests from browsers like Netscape and Internet Explorer and
then returns the appropriate HTML documents.
It is a computer with special software to host web pages and web
applications.
03/16/2025
Cont…
12 Web sever provides four major functions
Serving web pages
Running gateway programs (CGI) and returning output
Controlling access to the server
Monitoring and logging all access
Example of popular server are
Apache
IIS (Microsoft Internet Information Service)
Netscape Web server, …
03/16/2025
Cont...
13 Web Server and Browser Interaction
A Web Browser acts as an interface between the user and the Web server
and it carries out the following on behalf of the user
Contacts a web server
Sends a request for information
Receives the information and
Displays it on the user's computer
How Does a Web Server Accept Connections?
Accept network connections from browsers
Retrieve content from disk
Run local CGI programs
Transmit data back to clients be as fast as possible
03/16/2025
Support multi-threading and multi-processing
Internet Domain Names:
14 -DNS
The Domain Name System:
is a hierarchy of names.
Is "top-level domain names" (TLDs).
Are TLDs (EDU, COM, NET, ORG, GOV, MIL, and INT), and
the two letter country codes
03/16/2025
DNS:
15
Internet domain names come in four main types
1. top-level domains,
2. second-level domains,
3. third-level domains, and
4. country domains.
Internet domain names are an identifiers we use to refer to hosts on the
Internet.
03/16/2025
Top-level domains
16
TLD Usage
.bitnet Used for computers on the BITNET network.
.com Originally stood for "commercial" to indicate a site
used for commercial purposes, but it has since
become the most well-known top-level Internet
domain, and is now used for any kind of site.
.int Used by "International" sites, usually NATO sites.
.edu Used for educational institutions like universities.
.gov Used for Government sites.
.mil Used for Military sites.
.net Originally intended for sites related to the Internet
itself, but now used for a wide variety of sites.
.org Originally intended for non-commercial
"organizations", but now used for a wide variety of
03/16/2025
03/16/2025
Country Domains
18 A few countries and example domains are shown below.
Each country in the world has its own top-level Internet domain with a unique alphabetic designation.
03/16/2025
20
URL Defined
• Is a standard way developed to specify the location
of a resource available electronically.
03/16/2025
21
What are URLs?
• URL used to direct both people and software
applications to a variety of information, available
from a number of different Internet protocols.
• In short, a URL is a very convenient and succinct
way to direct people to a file or other electronic
resource.
• A URL is like your complete mailing address.
03/16/2025
22
Machine Path
03/16/2025
23
Machine Path
• http is the scheme
hyper text transfer protocol
• two slashes (//) separate the scheme from the
machine/domain name
• www.7sport.net is the machine/domain name
• single slash (/) separates the name from the path
• Finally 7sport/index.htm is the path.
03/16/2025
24
Machine Path
• sometimes the path will end in a slash (/)
• this indicates that the URL is not pointing to a
specific file
• in this case the server returns the "default" page
– homepage.html
– home.html
– welcome.html
– default.html
03/16/2025
25
– Examples:
– http://www.w3schools.com/html/html_forms.asp
(Hyper Text Transfer Protocol)
– ftp://garbo.uwasa.fi/pc/doc-net/
(File Transfer Protocol)
03/16/2025
26
03/16/2025
30
Scheme http://www.google.co.uk:80/search?hl=en&q=Football
03/16/2025
33
Chapter two
Web Page Development Using Html
Technical definitions:
A webpage is a single HTML document
A website is a collection of related Web Pages
Designing a good website requires more than just putting together a few pages.
Layout of web pages is very important
Poor layout makes for -
Difficult navigation
Hard to locate information on page
Visually unappealing
woldia university computer science department 03/16/2025
35 Some guidelines
Use the appropriate balance of images, color, and space.
◦ Too many images on one page (or large images) will be slow to load.
03/16/2025
36 Cont…
Have a common look and feel for each page in a web site.
There should be a sense of uniformity.
Perhaps there is a common graphical element and color on each page
Give the page a title (header).
The title shows up on the top line of the web site when viewed in a web browser.
Having no title indicates the designer doesn’t know beans about web design.
03/16/2025
37 Cont….
Utilize a common scheme for page navigation.
◦ Once you get on a page, there should be links back to the home page or other pages on the web site.
◦ example
◦ No links back to a home page indicates lack of skill in web design.
03/16/2025
38 Some Principles
Just because you can do it, doesn’t mean you should do it.
Know your audience and have a clear goal for your Web site.
03/16/2025
39 Steps to a well designed Web site
Have a goal
Target your audience
Create a plan
Select a Web service provider
Try it out
Maintain it
03/16/2025
Have
40
a goal for your web site
03/16/2025
Identify
41
your target audience
Who is the information for?
Do you have more than one audience?
Can you serve them all with one Web site?
What are the information needs of your audience?
What are their habits, characteristics, culture, technical
capabilities, etc.
Are they likely to start with the Web or another information
source?
03/16/2025
42
Identify your target audience(cont…)
Audience Analysis
Who are you trying to reach?
◦ Age
◦Language and Culture
◦Level of education
◦Access to the Web (High-speed? Dial-in?)
◦Familiarity with the Web
◦Barriers to access?
03/16/2025
43 Identify your target audience(cont…)
03/16/2025
Plan
44
it out
Identify information you already provide your audience.
Identify information that you haven’t, but would like to provide your audience.
Identify the sources of information you want to provide through your Web site.
Prepare that information for the web by collecting it and converting it.
Develop a vision for your Web site and storyboard it before construction begins.
Share your vision and storyboard with your colleagues and your bosses.
Estimate initial times and costs for construction.
Decide on the software/hardware tools necessary to construct your site.
03/16/2025
Select
45
a Web service provider
Coordinate the method for publishing and updating your Web pages.
mail files (transfer email messages from client to server )
FTP files(allow users to share files back and forth with one another over internet connection )
Know your root address (domain name).
Will you have a need for scripting and database interaction?
03/16/2025
46 Try it out: Optimize it for your audience
Test it in-house.
Test it on a sample audience.
Test it on as many different computers , monitors and
browsers as possible. Why?
Test it using various Internet connections.
Modems
Direct connects
03/16/2025
47 Make your site well-organized
03/16/2025
49 Make your site useful
In an organization, make this part of someone’s job. Pay them to do it. It’s worth it.
If a person is currently the “documentation person” or the “flier person,” consider
that person to be your Webmaster.
03/16/2025
51 Some more tips
What to do,
why to do it on the Web
Before we get started…
What sites are you working on?
What’s the purpose of your site?
What audience(s) are you trying to reach?
Site Planning
Determine site goals
Analyze your audience
Analyze the “competition”
Know your own abilities and resources
Map the current site
Design your new site
03/16/2025
52 Usable Web pages
Effective:
enable user to find required information
Efficient:
enable information to be found quickly without additional effort
Satisfying:
motivate user to come again and give appropriate image
To create a usable site it is essential to take a user-centered
approach to design.
03/16/2025
53 usability testing
The testers should…
Reflect your audience demographics
Be (relatively) un coached
The test should…
Have clearly defined goals
Be observed in some way
Ask for tester feedback
Notes
Post-test interviews
03/16/2025
54
What is HTML??
03/16/2025
55 What is HTML??
Appearance
Layout
Content(can’t enforce an exact look)
An HTML file must have an htm or html file extension.
Plain-text documents can be created using any text editor.
03/16/2025
57
HTML (Cont…)
One where we can embed special tags or formatting commands in the text.
03/16/2025
58 HTML (Cont…)
03/16/2025
59 Why do we use HTML?
Light
◦ Files are flat text and very small
◦ Quickly transferred over a network
Easy to learn
◦ Smaller and less complex than other markup languages
◦ Less tags (instructions)
Open standard
◦ Not proprietary
◦ Works with all platforms, all browsers, and all Web servers
The rise of Internet technologies
03/16/2025
60 Some Points
03/16/2025
HTML Document Structure
61
A HTML document consists of two major portions:
Head
Contains information about the document, like the title and “meta” data describing the contents.
Body
Contains the actual matter of the document.
Gets displayed within the browser window.
<!DOCTYPE HTML>
<html>
<head>
(Header: information about the page)
</head>
<body>
(Body: Web page content)
</body>
</html> 03/16/2025
62 Basic HTML syntax
HTML syntax
Tags
– Instructions for the browser
-Tags act like containers. They tell you something about the information that lies between their
opening and closing tags.
<tag> Some text</tag>
Nesting
– Close tags in the opposite order in which you opened them.
<tag1><tag2>Some text</tag2></tag1>
Attributes
– Specify attributes to use non-default behavior
<tag attribute=“value”>Text </tag>
03/16/2025
63
HTML Tags
HTML
<HTML> …… </HTML>
03/16/2025
65 HTML Tags (cont…)
HEAD
<HEAD >……</HEAD>
These tags are in the beginning of the document.
Important information is stored in-between these tags
including: title, meta-data, styles, and programming scripts.
Used to provide information about a web page.
Nests within itself other tags like
<base>, <meta> and <title>.
03/16/2025
66 HTML Tags (cont.…)
<base>
◦Attribute: href=url
◦Specifies the base pathname for all relative URLs in
the document.
◦No end tag.
<meta>
◦Used to provide information about a document.
◦It can be used to define the character set,
◦Keywords or descriptions to aid search engines.
◦example 03/16/2025
67 The use of meta tag
03/16/2025
68 HTML Tags (cont…)
TITLE
<TITLE>……</TITLE>
These tags are in-between the HEAD tags and contain the text that
appears in the title of the webpage.
Usually appears on the title bar of the browser window.
BODY
◦ <BODY>…..</BODY>
◦ As you may have guessed, the body tags contain all the text in the body of the
document.
◦ Used to bracket the body of a HTML document.
◦ Attributes:
background=url specifies an image file to be used as tiling background.
bgcolor=color Sets the background color of the document.
03/16/2025
text=color Sets the default color for the normal text in the document.
HTML Tags (cont…)
69
03/16/2025
70
Text Formatting in HTML
Paragraphs and Headings
<Hn> ……. </Hn>
◦ Used to generate headings, 1 ≤ n ≤ 6.
◦ Six different levels of headings.
<H1> is the largest, <H6> is the smallest.
<P>
◦ Paragraph marker, used to separate text into paragraphs.
◦ End tag </P> is optional if it is only one paragraph.
◦ A series of paragraph tags <p><p>…<p> with no intervening text is treated as a single <p>.
03/16/2025
71 Text Formatting in HTML(cont…)
<BR>
Used to indicate that the text following <BR> should
begin on the next line.
The amount of line spacing is less than that of a <P> break.
Example:
This is the first line. <br>
This is the second line. <br>
This is the third.
03/16/2025
72
Text Formatting in HTML(cont…)
<HR>
Produces a horizontal line, which can be used to delimit
sections.
Length of the line can be specified.
Examples:
<hr>
<hr size=“20”> [ no shade option possible ]
<hr width=“75%”>
<hr align=“right” width=120>
Across full width of browser, 20 pixels thick, 75% of available
page width, and 120 pixels right-justified.
03/16/2025
73
Text Formatting in HTML(cont…)
<address> ……. </address>
Supplies the contact information of the author.
Generally formatted in italics, with a line break above and below.
Example
<address>
Ato getachew worku <br>
Dept. of computer science<br>
woldia University <br>
Email: [email protected]
</address>
03/16/2025
74
Appearance of Text
<b> ……. </b>
Displays the enclosed text in bold.
<i> ……. </i>
Displays the enclosed text in italics.
<cite> ……. </cite>
Tells the browser that this is a citation. Usually displayed
in italics.
Example
<p><cite>The Scream</cite> by Edward Munch. Painted in 1893.</p>
03/16/2025
75
Appearance of Text (cont.….)
<pre> ……. </pre>
Allows browser to display carefully laid out text.
Used to display program listings.
Example:
<pre>
main()
{ int i, j;
abc ();
} </pre>
03/16/2025
76
Appearance of Text (cont.….)
<sub> ……. </sub>
Displays the enclosed text as subscript.
face = typeface
size = value [1 to 7; 3 is the default]
03/16/2025
77 Appearance of Text (cont.….)
03/16/2025
78
Summary
79
Internet Programming
Compiled by: SIMENEW SH.
Email: [email protected]
80
CSS selectors
are the heart and soul of CSS.
They define which HTML elements you are going to be manipulating with CSS
code and
The selector name creates a direct relationship with the HTML tag you want to
edit.
Example:
If you wanted to change the way a paragraph tag behaved, the CSS code would
look like:
p { PROPERTY: VALUE }
The above example is a template that you can use whenever you are
manipulating the paragraph HTML element
87 Creating CSS
Cascading Style Sheets come in three flavors:
internal,
external, and
inline.
Creating an inline style
All elements have a global attribute called style that can be used to provide an inline style.
An inline style is defined on the element to which you wish to add styling,
don’t need a selector; just need to specify the declaration block.
The following is an example of an inline style on the <body> element that sets the background color to
white and the font color to gray.
<body style='background-color: white; color: gray;'>
<body
</body> style='background-color: white; color: gray;'>
….
</body>
Inline CSS…
88
In the example, you don’t need an external style sheet because you defined the style on the
actual <body> element.
You should try to avoid this technique; it violates the primary goal of separation between
structure and presentation
doesn’t create any reusability because you will need to copy this style to each HTML document
you add to your application.
An advantage of using an inline style is
CSS code in classes will override the general CSS code for that element.
In the above example p.test1 overrides the style defined for the p p{ color: red; font-size:
20px; }
98 Using the universal selector
If you want to apply a style to every element, you can use the asterisk (*) symbol.
The following example applies the style to every element in the HTML document.
*{
You should avoid using the universal selector because of the performance cost.
background-color: white;
color: gray;
}
99 Grouping selectors
You can group selectors when you will be applying the same style by separating each selector
with a comma.
Button, p {
background-color: white;
color: gray;
}
100
CSS properties
Measurements used for positioning objects
101
Background Color
The background-color property specifies the background color of an element.
The background color of a page is defined in the body selector:
body {background-color:#b0c4de;}
With CSS, a color is most often specified by:
a HEX value - like "#ff0000"
an RGB value - like "rgb(255,0,0)"
a color name - like "red"
h1 {background-color:#6495ed;}
p {background-color:#e0ffff;}
div {background-color:#b0c4de;}
Background…
104
Background Image
The background-image property specifies an image to use as the background of an element.
By default, the image is repeated so it covers the entire element.
The background image for a page can be set like this:
body {background-image:url(‘astu.jpg');}
Background…
105
Background Image - Repeat Horizontally or Vertically
By default, the background-image property repeats an image
both horizontally and vertically.
body
{
background-image:url('gradient2.png');
background-repeat:repeat-x;
}
Background…
106
Background Image – Set position and no-repeat
The position of the image is specified by the background-
position property:
body
{
background-image:url('img_tree.png');
background-repeat:no-repeat;
background-position:right top;
}
Background…
107
Background - Shorthand property
To shorten the code, it is also possible to specify all the
properties in one single property. This is called a shorthand
property.
The shorthand property for background is simply
"background":
body {
background:url('img_tree.png') no-repeat right top;
}
Background…
108
color: color
h1 {text-decoration:overline}
h2 {text-decoration:line-through}
h3 {text-decoration:underline}
h4 {text-decoration:blink}
112 Text…
word-spacing property increases or decreases the white space
word-spacing:
betweenvalue
words.
Font Family
The font family of a text is set with the font-family property.
Note: If the name of a font family is more than one word, it
must be in quotation marks, like font-family: "Times New
Roman".
More than one font family is specified in a comma-separated
list:
Font…
119
Times New Roman
Georgia
Arial
Verdana
Courier New
Lucida Console
Monospace
Sans-serif
Eg:
p{
font-family:"Times New Roman", Times, serif;
}
Font…
120
Font Style
The font-style property is mostly used to specify italic text.
This property has three values:
normal - The text is shown normally
italic - The text is shown in italics
oblique - The text is "leaning" (oblique is very similar to
italic, but less supported)
p.normal {font-style:normal;}
p.italic {font-style:italic;}
p.oblique {font-style:oblique;}
Font
121
Font Size
The font-size property sets the size of the text.
Setting the text size with pixels, gives you full control over the text size:
Eg: (16px=1em)
h1 {font-size:40px;}
h2 {font-size:30px;}
p {font-size:14px;}
font-style: style {normal, italic, oblique}
font-weight: weight {normal, bold, bolder, lighter, 100, 200, …}
font-size: size
font-family: font_list (in order of precedence, separated by comma)
Borders
122
Box model
describes the rectangular boxes that are generated for elements
box model…
123
Margin - Clears an area around the border.
The margin does not have a background color, it is
completely transparent
Border - A border that goes around the padding and content.
The border is affected by the background color of the box
Padding - Clears an area around the content.
The padding is affected by the background color of the box
Content - The content of the box, where text and images
appear
Box model…
124
Width and Height of an Element
Important: When you set the width and height properties of an element with CSS, you just set
the width and height of the content area.
To calculate the full size of an element, you must also add the padding, borders and margins.
width:250px;
padding:10px;
border:5px solid gray;
margin:1
Width and Height…
125
The total width of an element should be calculated like this:
The last rule of the example above is equivalent to the example below:
BODY {
margin-top: 1em;
margin-right: 2em;
margin-bottom: 3em;
margin-left: 2em; /* copied from opposite side (right) */
}
Border properties
130
These properties set the width of the top, right, bottom, and
left border of a box
Border properties
border-top-width ={thin,thick,medium,length}
border-right-width
border-bottom-width
border-left-width
border-width
Border width
Border…
131
The border's thickness has an explicit value. Explicit border
widths cannot be negative
Eg:
H1{ border-width: thin } /* thin thin thin thin */
H1 { border-width: thin thick } /* thin thick thin thick */
H1 { border-width: thin thick medium }
Border color
border-top-color
border-right-color
border-bottom-color
border-left-color
border-color
Border…
132
border-style
The border-style property specifies what kind of border to display
None of the border properties will have ANY effect unless the border-
style property is set!
border-style values:
none: Defines no border
dotted: Defines a dotted border
dashed: Defines a dashed border
solid: Defines a solid border
double: Defines two borders. The width of the two borders are the
same as the border-width value
Border style values…
133
groove: Defines a 3D grooved border. The effect depends on the
border-color value
ridge: Defines a 3D ridged border. The effect depends on the border-
color value
inset: Defines a 3D inset border. The effect depends on the border-
color value
outset: Defines a 3D outset border. The effect depends on the border-
color value
Border style…
134
border:<border-width>|<border-style>|<color>
The 'border' property is a shorthand property for setting the
same width, color, and style for all four borders of a box.
border-width
border-style (required)
border-color
border:5px solid red;
Example
</style>
Example
</head>
<!DOCTYPE
137 html> <body>
<html> <p class="none">No border.</p>
<p class="dotted">A dotted border.</p>
<head>
<p class="dashed">A dashed border.</p>
<style> <p class="solid">A solid border.</p>
p.none {border-style:none;} <p class="double">A double border.</p>
<p class="groove">A groove border.</p>
p.dotted {border-style:dotted;}
<p class="ridge">A ridge border.</p>
p.dashed {border-style:dashed;} <p class="inset">An inset border.</p>
p.solid {border-style:solid;} <p class="outset">An outset border.</p>
p.double {border-style:double;} <p class="hidden">A hidden border.</p>
</body>
p.groove {border-style:groove;} </html>
p.ridge {border-style:ridge;}
p.inset {border-style:inset;}
Border…
138
Unlike the shorthand 'margin' and 'padding' properties, the 'border' property cannot set different
values on the four borders. To do so, one or more of the other border properties must be used.
For example, the first rule below is equivalent to the set of four rules shown after it:
Fixed Positioning
An element with fixed position is positioned relative to the browser window.
It will not move even if the window is scrolled:
Example
p.pos_fixed
{
position:fixed;
top:30px;
right:5px;
}
142 Position…
Overlapping Elements
When elements are positioned outside the normal flow, they can overlap other
elements.
The z-index property specifies the stack order of an element (which element should
be placed in front of, or behind, the others)
An element can have a positive or negative stack order:
Example
img
{
position:absolute;
left:0px;
top:0px;
z-index:-1;
}
Example
<!DOCTYPE html>
143
<html>
<head><style>
img
{
position:absolute;
left:0px;
top:0px;
z-index:-1;
}
</style></head>
<body>
<h1>This is a heading</h1>
<img src="w3css.gif" width="100" height="140" />
<p>Because the image has a z-index of -1, it will be placed behind the text.</p>
</body>
144 CSS Lists
The CSS list properties allow you to:
Set different list item markers for ordered lists
Set different list item markers for unordered lists
Set an image as the list item marker
List
In HTML, there are two types of lists:
unordered lists - the list items are marked with bullets
ordered lists - the list items are marked with numbers or letters
With CSS, lists can be styled further, and images can be used as the list item marker.
145 CSS List…
Inside Indents the marker and the text. The bullets appear
inside the content flow
Outside Keeps the marker to the left of the text. The bullets
appears outside the content flow. This is default
149 Style Sheet Layers
CSS gives you an opportunity to create layers of various divisions. The CSS layers refer
to applying the z-index property to elements that overlap with each other.
The z-index property is used along with the position property to create an effect of layers.
You can specify which element should come on top and which element should come at
bottom.
A z-index property can help you to create more complex webpage layouts. The following
example shows how to create layers in CSS.
150
Thank You
see you next class!
151
151
Chapter 4
Internet Programming
152
Java script
153 JavaScript History and Versions
JavaScript was introduced as part of the Netscape 2.0
browser
Microsoft soon released its own version called JScript
(European Computer Manufacture Association) ECMA
developed a standard language known as ECMAScript
ECMAScript Edition 3 is widely supported and is what
we will call “JavaScript”
154 What is JavaScript
<html>
<head><title>JavaScript HelloWorld!</title></head>
<body>
<script language="JavaScript">
document.write(“welcome to javaScript”)
</script>
</body>
</html>
157 What is JavaScript
<html>
<head>
<script language=“JavaScript”>
document.write (“<b> This is first </b>);
</script>
</head>
<body>
Now where does this print on the web page???? <br />
This is first
Now where does this print on the web page????
<script language=“JavaScript”>
document.writeThis might
( “This be last?
might be last?”)
</script>
159 What is JavaScript
Now, let JavaScript generate HTML for us…
<html>
<head><title>JavaScript HelloWorld!</title></head>
<body>
<script laguage="JavaScript">
document.write("<h2>Javascript-Generated output:</h2>"
+ "<p>This paragraph generated by JavaScript</p>"
+ "<p>It can even insert an image</p>"
+ "<img src=‘ASTU.jpg' />")
</script>
</body>
</html>
160 JavaScript Comment
The JavaScript comments are meaningful way to deliver message.
It is used to add information about the code, warnings or suggestions so that end user can
easily interpret the code.
The JavaScript comment is ignored by the JavaScript engine i.e. embedded in the browser.
There are mainly two advantages of JavaScript comments.
To make code easy to understand the code
To avoid the unnecessary code It can also be used to avoid the code being executed.
161 Types of JavaScript Comments
Selection statements
Loop statements
<html>
<head><title>JavaScript</title></head>
<body>
<script laguage="JavaScript">
var variable1 = 1; var variable2 = 2;
if(variable1 >= 0){
document.write("<p> 1 is greater than 0 </p>");
}
document.write("<p>Resuming execution after 'if' statement</p>");
else if(variable1 > variable2){
document.write("<p>1 is greater than 2</p>");
}
else {
document.write("<p>2 is greater than 1</p>");
}
171 JavaScript Switch
The JavaScript switch statement is used to execute one code from multiple expressions.
The syntax of JavaScript switch statement is given below.
switch(expression){
case value1:
code to be executed;
break;
case value2:
code to be executed;
break;
......
default:
code to be executed if above values are not matched;
}
172 Switch example
<script type="text/javascript">
var x = window.prompt(" enter a no. less than 2");
x= Number(x)
switch (x)
{
case 0: document.write("zero") ; break ;
case 1: document.write("one") ; break ;
default: document.write("not valid")
}
</script>
173 Working with Program Loops
The JavaScript for loop iterates the elements for the fixed number of times. The syntax of
for loop is given below.
for (initialization; condition; increment)
{
code to be executed
}
Example
<script>
for (i=1; i<=5; i++)
{
document.write(i + "<br/>")
}
</script>
175 Working with Program for Loops
<table border = 2>
<tr>
<script type = "text/javascript">
for (num = 1; num <= 4; num ++){
document.write("<td>" + num + "</td>");
}
</script>
</tr>
</table>
176 Working with Program nested for
<table borderLoops
= 2> Nesting a For loop
<tr>
<script type = "text/javascript">
num = 1;
while(num <= 4){
document.write("<td>" + num + "</td>");
num ++;
}
</script>
</tr>
</table>
179 Working with Program Loops
<table border = 2>
<script type = "text/javascript">
num = 1;
while(num <= 4){
document.write("<tr>");
J = 1;
while(J <= 4){
document.write("<td>" + num + J + "</td>");
Nesting a While loop
J ++;
}
document.write("</tr>");
num ++;
}
</script>
</table>
180 3) JavaScript do while loop
The JavaScript do while loop iterates the elements for the infinite number of times like while
loop. But, code is executed at least once whether condition is true or false. The syntax of do while
loop is given below.
do{
code to be executed
}while (condition);
Example
<script>
var i=21;
do{
document.write(i + "<br/>");
i++;
}while (i<=25);
</script>
181 JavaScript Functions
Abstraction
is used to hide certain details and only show the
essential features of the object.
Functions allows to reduce the size of program
It allows writing functionality once, then reuse it
Encapsulation
is a mechanism of wrapping the data (variables)
and code acting on the data (methods) together as
a single unit.
Function name allows us to access a function in our
182 JavaScript Functions
return return_value;
}
JavaScript Functions can have 0 or more arguments.
184 JavaScript Functions -- Parameters
Example,
function findMaxValue(num1, num2, num3)
185 JavaScript Functions
<head>
<script language=“JavaScript”>
</script>
</head>
186 JavaScript Functions – Local Variables
local variable is visible only within the function body after it’s
declared
function myFunction() {
var carName = “Corolla";
}
187 JavaScript Functions – Local
Variables
Write a function that returns the max of three numbers
function maxNum(num1, num2, num3){
var max = 0;
if((num1 >= num2) && (num1 >= num3)){
max = num1;
}
else if((num2 >= num1) && (num2 >= num3)){
max = num2;
}
else{
max = num3;
}
return max;
}
document.write(maxNum.apply(this, arr));
188 JavaScript Functions – function calling
[idf=] functionName([Actual_parameter_list]);
var x = 1, y = 4, z = 2;
<html>
<body>
<script>
function getcube(number){
alert(number*number*number);
}
</script>
<form>
<input type="button" value="click" onclick="getcube(4)"/>
</form>
</body>
</html>
190 JavaScript Functions - Return
Return keyword tells function to return some value and exit
immediately
Syntax:
return expression;
Function can have multiple return statements but only the first can be
executed in any given function call
<html>
<body>
<script>
function getInfo(){
return "hello javascript! How r u?";
}
</script>
<script>
document.write(getInfo());
</script>
</body>
</html>
192 JavaScript Functions – Global
Variables
Global variables are those declared outside of
functions
Are visible throughout the program
function writeHello() {
document.write(globalHello);
}
// outputs “Hello!”
193 JavaScript Functions
Example of Confirm
Built-In Functions
<script type="text/javascript">
Prompt
function msg(){
Alert var v= confirm("Are u sure?");
Confirm if(v==true){
alert("ok");
}
else{
alert("cancel");
}
}
</script>
JavaScript objects
195 Object Introduction
A javaScript object is an entity having state and behavior (properties
and method).
Everything" in JavaScript is an Object: a String, a Number, an Array, a
Function....
An object is a set of properties
A property consists of a unique name (within an object) with an
associated value
The type of a property depends on the type of its value and can vary
dynamically.
An object is just a special kind of data, with properties and
methods.
There are no classes in JavaScript
196 Object Creation
Objects are created using new expression
A constructor is a function
new object([argument list])
When called via new expression, a new empty Object is created and passed to the constructor along
with the argument values
Constructor performs initialization on object
There are 3 ways to create objects.
By object literal
By creating instance of Object directly (using new keyword)
By using an object constructor (using this keyword)
197 Object Methods
Methods are the actions that can be performed on objects.
Creating objects in JavaScript using the constructor function
function rect (length, width){
this.length = length;
this.width = width;
}
One can use the new keyword and call our rect constructor function
myrect = new rect (100, 200);
To add methods to your objects, first define a function
Then associate that function with a particular object.
function makeLonger() {
this.length += 5;
198
THE END!!!
Thank you