Web
Web
P R OG R A M M IN G
IN T E R N E T A N D W W W
1 - O V E RV IE W O F THE
CHAPTER
Basic Concepts of Internet and WWW
• Network is communication system connecting two or more computers.
• Internet is a worldwide network that connects smaller independent networks
worldwide.
• Internet could be connected by network cables or through satellite links.
• The communication is governed by protocols established by an international body -
Internet Engineering Task Force.
• The Internet connectivity is provided by Internet Service Providers (ISP).
• These corporations dedicate computers to act as servers - that is they make
information (such as Web pages or e-mail) available to users of the Internet.
• The origin of Internet devised from the concept of Advanced Research Project
Agency Network (ARPANET) developed by United States Department of Defence.
• The World Wide Web (WWW), commonly known as the Web, is an information
system enabling documents and other web resources to be accessed over the
Internet.
• Servers and resources on the World Wide Web are identified and located through
• The original and still very common document type is a web page formatted in
Hypertext Markup Language (HTML).
• This mark-up language supports plain text, images, embedded video and audio contents, and
scripts (short programs) that implement complex user interaction.
• The HTML language also supports hyperlinks (embedded URLs) which provide immediate
access to other web resources.
• Web navigation, or web surfing, is the common practice of following such
hyperlinks across multiple websites.
• The information in the Web is transferred across the Internet using the Hypertext
Transfer Protocol (HTTP).
• Multiple web resources with a common theme and usually a common domain name
make up a website.
• A single web server may provide multiple websites, while some websites, especially
the most popular ones, may be provided by multiple servers.
• The World Wide Web has become the world's dominant software platform billions of
people worldwide use to interact with the Internet.
• The Internet is a global system of computer networks interconnected through
telecommunications and optical or satellite networking.
• In contrast, the World Wide Web is a global collection of documents and other
resources, linked by hyperlinks and URIs.
• A Uniform Resource Identifier (URI) is a unique sequence of characters
that identifies a logical or physical resource used by web technologies.
• URIs may be used to identify anything, including real-world objects, such as
people and places, concepts, or information resources such as web pages and
books.
• URI identifies a resource by location (URL), name (URN), or both.
• URL and URN are subsets of URI.
• URI defines the identity of a resource and establishes the unique distinguished
identity of the resource regardless of the method used (name, location or both).
• URI syntax was first defined in RFC 2396, published in August 1998, and
finalized in RFC 3986, published in January 2005.
• The URI generic syntax consists of five components organized hierarchically in
order of decreasing significance from left to right:
URI = scheme ":" ["//" authority] path ["?" query] ["#" fragment]
• A Uniform Resource Name (URN) is a URI that identifies a resource by
name in a particular namespace.
• A URN may be used to talk about a resource without implying its location or how
to access it.
• For example, in the International Standard Book Number (ISBN) system, urn:
ISBN: 0-486-27557-4.
• However, it gives no information as to where to find a copy of that book.
• A Uniform Resource Locator (URL) is a URI that specifies the means of acting
upon or obtaining the representation of a resource, i.e. specifying both its primary
access mechanism and network location.
• For example, the URL http://example.org/wiki/Main_Page refers to a resource identified as
/wiki/Main_Page, whose representation is obtainable via the Hypertext Transfer Protocol (http
:) from a network host whose domain name is example.org.
• A URN is analogous to a person's name, while a URL is analogous to their street
address. In other words, a URN identifies an item and a URL provides a method
for finding it.
HTTP protocol
• Reference Model offers a means of standardization which is acceptable
worldwide.
• Since people using the computer network are located over a wide physical
range and their network devices might have heterogeneous architecture.
• In order to provide communication among heterogeneous devices, we need a
standardized model i.e. a reference model, which would provide us way how
these devices can communicate regardless their architecture.
• We have two reference models such as OSI model and TCP/IP reference
model. OSI model is a hypothetical one but the TCP/IP is practical model and
is used in the Internet.
• Application Layer is one of the layers of the TCP/IP model.
• An application layer is the topmost layer in the TCP/IP model.
• It is responsible for handling high-level protocols.
• This layer allows the user to interact with the application.
• Every application cannot be placed inside the application layer except those that have
interaction with the communication system.
• For example: text editor cannot be considered in application layer while web browser is, because
it uses HTTP protocol to interact with the network where HTTP protocol is an application layer
• HTTP stands for Hypertext transfer protocol.
• Hypertext is text which contains links to other texts.
• This protocol allows us to access the data over the World Wide Web.
• It transfers the data in the form of plain text, audio, video.
• HTTP functions as a request–response protocol in the client–server
model.
• A web browser, for example, may be the client whereas a process,
named web server, running on a computer hosting one or more websites
may be the server.
• The client submits an HTTP request message to the server.
• The server, which provides resources such as HTML files and other
content or performs other functions on behalf of the client, returns a
response message to the client.
• The response contains completion status information about the request
Web browsers
• Documents and downloadable media are made available to the network through web
servers and can be accessed by programs such as web browsers.
• A web browser is an application for accessing websites.
• When a user requests a web page from a particular website, the browser retrieves its
files from a web server and then displays the page on the user's screen.
• A web browser is not the same thing as a search engine.
• A search engine is a website that provides links to other websites.
• However, to connect to a website's server and display its web pages, a user must
have a web browser installed.
• In some technical contexts, browsers are referred to as "user agents.“
• If the URL uses the secure mode of HTTP (HTTPS), the connection between the
browser and the web server is encrypted for the purposes of communications
security and information privacy.
• During the course of browsing, cookies received from various websites are stored by
the browser.
• Cookies are small pieces of text sent to your browser by a website you visit.
• They help that website remember information about your visit, which can both make
it easier to visit the site again and make the site more useful to you.
January 2023 desktop browser share
Client-Server model
• The client–server model is a distributed application structure that partitions
tasks or workloads between the providers of a resource or service, called
servers, and service requesters, called clients.
• Often clients and servers communicate over a computer network on separate
hardware, but both client and server may reside in the same system.
• A server host runs one or more server programs, which share their resources
with clients.
• A client usually does not share any of its resources, but it requests content or
service from a server.
• Clients, therefore, initiate communication sessions with servers, which await
incoming requests.
• Examples of computer applications that use the client–server model are email,
network printing, and the World Wide Web.
• The "client–server" characteristic describes the relationship of cooperating
programs in an application.
• The server component provides a function or service to one or many clients,
which initiate requests for such services.
• Servers are classified by the services they provide.
• For example, a web server serves web pages and a file server serves computer files.
• The sharing of resources of a server constitutes a service.
• Whether a computer is a client, a server, or both, is determined by the nature
of the application that requires the service functions.
• For example, a single computer can run a web server and file server software at the
same time to serve different data to clients making different kinds of requests.
• Generally, a service is an abstraction of computer resources and a client does
not have to be concerned with how the server performs while fulfilling the
request and delivering the response.
• The client only has to understand the response based on the well-known
application protocol, i.e. the content and the formatting of the data for the
requested service.
• Clients and servers exchange messages in a request–response messaging
pattern.
• The client sends a request, and the server returns a response.
• This exchange of messages is an example of inter-process communication.
• To communicate, the computers must have a common language, and they must follow
rules so that both the client and the server know what to expect.
• All protocols operate in the application layer.
• The application layer protocol defines the basic patterns of the dialogue.
• To formalize the data exchange even further, the server may implement an
application programming interface (API).
• The API is an abstraction layer for accessing a service.
• By restricting communication to a specific content format, it facilitates cross-platform data
exchange.
• A server may receive requests from many distinct clients in a short period.
• A computer can only perform a limited number of tasks at any moment, and
relies on a scheduling system to prioritize incoming requests from clients to
accommodate them.
• To prevent abuse and maximize availability, the server software may limit the
availability to clients.
• Denial of service attacks are designed to exploit a server's obligation to
process requests by overloading it with excessive request rates.
• Encryption should be applied if sensitive information is to be communicated between the
client and the server.
Example
• When a bank customer accesses online banking services with a web
browser (the client), the client initiates a request to the bank's web
server.
• The customer's login credentials may be stored in a database, and the
webserver accesses the database server as a client.
• An application server interprets the returned data by applying the bank's
business logic and provides the output to the webserver.
• Finally, the webserver returns the result to the client web browser for
display.
• A host is any computer connected to a network.
• Whereas the words server and client may refer either to a computer or to
a computer program, server-host and client-host always refer to
computers.
• The host is a versatile, multifunction computer; clients and servers are
Web programming
• Web programming refers to the writing, markup and coding involved in Web
development, which includes Web content, Web client and server scripting and
network security.
• It is the creation of an application that works over the internet i.e. websites.
• The most common languages used for Web programming are XML, HTML,
JavaScript, Perl 5 and PHP.
• Web programming is different from just programming, which requires interdisciplinary
knowledge on the application area, client and server scripting, and database
technology.
• Web programming can be briefly categorized into client and server coding.
• The client side needs programming related to accessing data from users and providing
information.
• It also needs to ensure there are enough plugins to enrich user experience in a graphic user
interface, including security measures.
• To improve user experience and related functionalities on the client side, JavaScript is usually
used.
• It is an excellent client-side platform for designing and implementing Web applications.
• HTML5 and CSS3 supports most of the client-side functionality provided by other application
frameworks.
Frontend Development
• The part of a website where the user interacts directly is termed as front end. It
is also referred to as the ‘client side’ of the application.
• Here are some of the most common front-end languages:
• HTML
• HTML (Hypertext Markup Language) is among the basic web development technologies for
designing the client side of the app.
• Additionally, it is the standard front-end language used to describe the structure of a webpage.
• Front-end developers can use HTML to create static pages consisting of text, images, headings,
links, and tables.
• CSS
• CSS (Cascading Style Sheets) is a language that allows us to stylize HTML elements. It basically
makes it easy to create beautiful web pages.
• JavaScript
• JavaScript or JS is one of the essential web technologies.
• It is a popular scripting language used to make a web app or site interactive.
• For example, JavaScript allows us to add interactive elements like buttons, visualizations, and
animations.
• AJAX
• Ajax stands for Asynchronous Javascript and XML.
• It allows us to communicate with the server without the need to refresh the web page. As a result, it
improves performance and user experience.
Backend Development
• Back-end development means working on server-side software, which focuses on
everything you can't see on a website.
• Back-end Development refers to the server-side development. It focuses on
databases, scripting, and website architecture.
• PHP
• PHP is a popular server-side scripting language used for web development. It is an open-source language that
supports several databases. For instance, you can use PHP to connect to and manage a database
management system like MySQL.
• Node.js
• Node.js is an open-source runtime environment that allows developers to execute JS code outside a web
browser.
• Python
• Python is another very popular open-source, object-oriented programming language used for several
purposes. It can work on multiple platforms like Windows, Linux, and Mac. Additionally, one of the most liked
features of Python is its human-friendly syntax, which makes it easy to learn and write clean code.
• Ruby
• Developed in the 1990s, Ruby is a general-purpose, object-oriented programming language. It also has a
simple syntax, making it super easy to learn and write.
• Java
• Java is another widely used object-oriented programming language. Released in 1995 by Sun Microsystems,
Java is highly scalable and easy to use.
• JavaScript
• We can use JavaScript programming language for both front-end and back-end.
INT E R N E T
P R OG R A M M IN G
O D U C T IO N TO H T ML
CHAPTER 2: INT R
What is HTML
• HTML (Hyper Text Markup Language) is a language in which most websites
are written.
• HTML is used to create pages and make them functional.
• HTML was first created by Tim Berners-Lee, Robert Cailliau, and others
starting in 1989.
• Hypertext means that the document contains links that allow the reader to
jump to other places in the document or to another document altogether.
• The latest version is known as HTML5.
• A Markup Language is a way that computers speak to each other to control
how text is processed and presented.
• To do this, HTML uses two things: tags and attributes.
• Originally, HTML was developed with the intent of defining the structure of
documents like headings, paragraphs, lists, and so forth to facilitate the
sharing of scientific information between researchers.
• Now, HTML is being widely used to format web pages with the help of
different tags available in HTML language.
Tags and Attributes
• HTML documents imply a structure of nested HTML elements.
• These are indicated in the document by HTML tags, enclosed in angle
brackets.
• Tags and attributes are the basis of HTML.
• An element is indicated by a pair of tags: a "start tag" < > and "end tag" </
>.
• The text content of the element is placed between these tags.
• Tags may also enclose further tag markup between the start and end,
including a mixture of tags and text.
• This indicates further (nested) elements, as children of the parent element.
• The start tag may also include the element's attributes within the tag.
• These indicate other information, such as identifiers for sections within the
document, identifiers used to bind style information to the presentation of the
document.
• Some elements, such as the line break <br> do not permit any embedded
content, either text or further tags.
• The general form of an HTML element is:
• <tag attribute1="value1" attribute2="value2">''content''</tag>
• Example
• <body bgcolor=“tomato”>Main content</body>
• The name of an HTML element is the name used in the tags.
• Note that the end tag's name is preceded by a slash character, /, and that in
empty elements the end tag is neither required nor allowed.
• If attributes are not mentioned, default values are used in each case.
• Attributes usually come in name/value pairs like: name="value".
• Web pages can be created and modified by using HTML editors.
• You can use a simple text editor like Notepad (for Windows), TextEdit (for
Mac), or Sublime Text 3, Notepad ++, Komodo Edit or any other.
• You choose your HTML editor based on cross-platform support, Auto-
completion feature, Plugin options for extended functionalities, Coding
interface.
Steps in to HTML programming
• Step 1: Open your choice of HTML editor
• Step 2: Write Some HTML
• All HTML documents must start with a document type declaration: <!DOCTYPE html>. The
<!DOCTYPE> declaration represents the document type, and helps browsers to display web
pages correctly. It must only appear once, at the top of the page (before any HTML tags). The
<!DOCTYPE> declaration is not case sensitive.
• Step 3: Save the HTML Page using .htm or .html extension.
• Step 4: View the HTML Page in Your Browser
HTML Heading
HTML Paragraphs
The HTML <pre>
HTML Styles
HTML Text Formatting
Elements
Tag Description
<b> Defines bold text
<em> Defines emphasized text
<i> Defines a part of text in an alternate
voice or mood
</style>
</head>
<body>
</body>
</html>
The output…
• Ordered list, header, link and style ID
<ul>
<h1 id="myHeader">MY COUNTRY - ETHIOPIA</h1>
<li><a href="#home"> Home</a></li>
<li><a href="#news"> News</a></li>
<li><a href="#contact"> Contact</a></li>
<li><a href="#about"> About</a></li>
</ul>
#myHeader {
background-color: DodgerBlue;
color: black;
padding: 20px;
text-align: left;
}
The output…
• Copy all the content from the home page, create new page and
paste. Then change the name of the page as in the following list.
<ul>
<h1 id="myHeader" >MY COUNTRY - ETHIOPIA</h1>
<li><a href="new 1.html"> Home</a></li>
<li><a href="news.html"> News</a></li>
<li><a href="contact.html"> Contact</a></li>
<li><a href="about.html"> About</a></li>
</ul>
<style>
ul { list-style-type: none;
background-color:DodgerBlue;
overflow:hidden;
}
li a{ color:black;
display:block;
padding:16px;
}
li { float:left; }
li a:hover { background-color:white; }
</style>
• Div, h2, style attribute, align attribute, span
<div style="background-
color:#454545;color:white;padding:20px;">
<h2 align="center">
<span style="color:#39FF14;"> Eth </span>
<span style="color:yellow;"> io </span>
<span style="color:red;"> pia </span>
</h2>
<p>
Ethiopia, officially the Federal Democratic Republic of Ethiopia,
is a landlocked country in the Horn of Africa.
</p>
</div>
Form elements
<form action="#" style="background-color:lightblue; width:30%;">
<h3>Fill in your profile here</h3>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" required><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" required><br>
Male:<input type="radio" id="male" name="gender" value="Male" required>
Female<input type="radio" id="female" name="gender" value="Female"
required> <br>
Programming:<input type="checkbox" id="programming"
name="programming" value="Programming" >
Database:<input type="checkbox" id="database" name="database"
value="Database" checked> <br>
</form>
Form elements
<form action="#" style="background-color:lightblue; width:30%;">
Favorite Color:<input type="color" id="favcolor" name="favcolor"><br>
Birth Date:<input type="date" id="datemax" name="datemax" max="2000-12-
31"><br>
Phone no.<input type="tel" id="phone" name="phone" placeholder="123-45-
679" pattern="[0-9]{3}-[0-9]{2}-[0-9]{3}"> <br>
Email:<input type="email" id="email" name="email"><br>
<label for="pwd">Password:</label><br>
<input type="password" id="pwd" name="pwd"><br><br>
Attachment: <input type="file" id="myfile" name="myfile"><br>
<input type="submit" value="Submit"><input type="reset" value="Reset">
</form> label {
display:inline-block;
width:150px;
font-weight:bold;
}
<iframe src="Test.html" style="border:none;" height="300" width="100%"
title="Iframe Example"></iframe>
CA SC A D E D S T Y L E
SHEET S - C S S
CHAPTER - 3
Introduction
• CSS is the language we use to define styles for your web pages,
including the design, layout and variations in display for different
devices and screen sizes.
• A CSS rule consists of a selector and a declaration block.
• Example
• div {
overflow-x: hidden; /* Hide horizontal scrollbar */
overflow-y: scroll; /* Add vertical scrollbar */
}
• The float property is used for positioning and formatting content e.g. let
an image float left to the text in a container.
• The float property can have one of the following values:
• left - The element floats to the left of its container
• right - The element floats to the right of its container
• none - The element does not float (will be displayed just where it occurs in the
text). This is default
• inherit - The element inherits the float value of its parent
• In its simplest use, the float property can be used to wrap text around
images.
Example:
img {
float: right;
}
<style>
div {
float: left;
padding: 15px;
}
.div1 { background: red; }
.div2 { background: yellow; }
.div3 { background: green;}
</style>
<body>
<div class="div1">Div 1</div>
<div class="div2">Div 2</div>
<div class="div3">Div 3</div>
</body>
JavaScript
Chapter - 4
Introduction
• As of 2022, 98% of websites use JavaScript on the client side for
webpage behavior, often incorporating third-party libraries.
• All major web browsers have a dedicated JavaScript engine to execute
the code on users' devices.
• JavaScript It is multi-paradigm, supporting event-driven, functional,
and imperative programming styles.
• It has application programming interfaces (APIs) for working with text,
dates, regular expressions, standard data structures, and the Document
Object Model (DOM).
• An application programming interface (API):
• is a way for two or more computer programs to communicate with each other.
• It is a type of software interface, offering a service to other pieces of software
• In contrast to a user interface, which connects a computer to a person, an
application programming interface connects computers or pieces of software to
each other.
• The Document Object Model (DOM):
• is a programming interface for HTML(HyperText Markup
Language) and XML(Extensible markup language) documents.
• With DOM, we can easily access and manipulate tags, IDs, classes, Attributes, or
Elements of HTML using commands or methods provided by the Document
object.
• Using DOM, the JavaScript gets access to HTML as well as CSS of the web page
and can also add behavior to the HTML elements.
• so basically Document Object Model is an API that represents and interacts
with HTML or XML documents.
• Although Java and JavaScript are similar in name, syntax, and
respective standard libraries, the two languages are distinct and differ
greatly in design.
• JavaScript and Java are completely different languages, both in concept
and design.
• JavaScript was invented by Brendan Eich in 1995, and became an ECMA
standard in 1997.
• ECMA-262 is the official name of the standard. ECMAScript is the official name of
the language.
• JavaScript was initially created to “make web pages alive”.
• The programs in this language are called scripts.
• They can be written right in a web page’s HTML and run automatically as the
page loads.
• Scripts are provided and executed as plain text.
• They don’t need special preparation or compilation to run.
• When JavaScript was created, it initially had another name: “LiveScript”.
• But Java was very popular at that time, so it was decided that positioning a
new language as a “younger brother” of Java would help.
• But as it evolved, JavaScript became a fully independent language with its
own specification called ECMAScript, and now it has no relation to Java at
all.
• ECMAScript is a JavaScript standard intended to ensure the interoperability of web
pages across different browsers.
• It is standardized by Ecma International in the document ECMA-262.
• Ecma International is a nonprofit standards organization for information and
communication systems.
• It acquired its current name in 1994, when the European Computer Manufacturers
Association (ECMA) changed its name to reflect the organization's global reach and
activities.
• As a consequence, the name is no longer considered an acronym and no longer uses
Starting with JavaScript
• In HTML, JavaScript code is inserted between <script> and </script> tags.
• Old JavaScript examples may use a type attribute: <script
type="text/javascript">.
• The type attribute is not required. JavaScript is the default scripting language in
HTML.
• You can place any number of scripts in an HTML document.
• Scripts can be placed in the <body>, or in the <head> section of an HTML
page, or in both.
• Placing scripts at the bottom of the <body> element improves the display
speed, because script interpretation slows down the display.
• Scripts can also be placed in external files.
• External scripts are practical when the same code is used in many different
web pages.
• JavaScript files have the file extension .js.
• To use an external script, put the name of the script file in the src (source)
attribute of a <script> tag:
• <script src="myScript.js"></script>
• You can place an external script reference in <head> or <body> as you like.
• The script will behave as if it was located exactly where the <script> tag is
located.
• External scripts cannot contain <script> tags.
• Placing scripts in external files has some advantages:
• It separates HTML and code
• It makes HTML and JavaScript easier to read and maintain
• Cached JavaScript files can speed up page loads
• To add several script files to one page - use several script tags.
• <script src="myScript1.js"></script>
<script src="myScript2.js"></script>
Example 1 - JavaScript Can Change HTML Content
One of many JavaScript HTML methods is getElementById().
<!DOCTYPE html>
<html>
<body>
<p id="demo">JavaScript can change HTML content.</p>
<button type="button" onclick='document.getElementById("demo").innerHTML
= "Hello JavaScript!"'>Click Me!</button>
</body>
</html>
Example 2 - JavaScript Can Change HTML Attributes
<!DOCTYPE html>
<html>
<body>
<p>In this case JavaScript changes the value of the src (source)
attribute of an image.</p>
<button
onclick="document.getElementById('myImage').src='pic_bulbon.gif'">
Turn on the light</button>
<img id="myImage" src="pic_bulboff.gif" style="width:100px">
<button
onclick="document.getElementById('myImage').src='pic_bulboff.gif'">
Turn off the light</button>
</body>
</html>
Example 3 - JavaScript Can Change HTML Styles (CSS)
<!DOCTYPE html>
<html>
<body>
<h2>What Can JavaScript Do?</h2>
<p id="demo">JavaScript can change the style of an HTML element.</p>
<button type="button"
onclick="document.getElementById('demo').style.fontSize='35px'">Click
Me!</button>
</body>
</html>
Example 4 - JavaScript Can Hide HTML Elements
<!DOCTYPE html>
<html>
<body>
<h2>What Can JavaScript Do?</h2>
<p id="demo">JavaScript can hide HTML elements.</p>
<button type="button"
onclick="document.getElementById('demo').style.display='none'">Click
Me!</button>
</body>
</html>
The HTML DOM Tree of Objects
• The DOM is a W3C (World Wide Web Consortium) standard.
• The DOM defines a standard for accessing documents.
• The W3C Document Object Model (DOM) is a platform and language-neutral interface that
allows programs and scripts to dynamically access and update the content, structure, and
style of a document. The W3C DOM standard is separated into 3 different parts:
• Core DOM - standard model for all document types
• XML DOM - standard model for XML documents
• HTML DOM - standard model for HTML documents
• The HTML DOM can be accessed with JavaScript (and with other programming languages).
• In the DOM, all HTML elements are defined as objects.
• The programming interface is the properties and methods of each object.
• A property is a value that you can get or set (like changing the content of an HTML
element).
• A method is an action you can do (like add or deleting an HTML element).
• The innerHTML Property
• The easiest way to get the content of an element is by using the innerHTML property.
• The innerHTML property is useful for getting or replacing the content of HTML elements.
• The innerHTML property can be used to get or change any HTML element, including <html> and
<body>.
• JavaScript HTML DOM Document
• The HTML DOM document object is the owner of all other objects in your web page.
• The document object represents your web page.
<!DOCTYPE html>
<html>
<body>
<p id="intro"></p>
<p id="demo"></p>
<p class="intro"></p>
<script>
document.getElementById("demo").innerHTML = "Paragraph by element";
const x = document.getElementsByClassName("intro");
x[0].innerHTML="Paragraph by class";
const y = document.getElementsByTagName("p");
y[0].innerHTML = "Paragraph by tag";
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<form id="frm1" action="/action_page.php">
First name: <input type="text" name="fname" value="Abebe"><br>
Last name: <input type="text" name="lname" value="Tola"><br><br>
<input type="submit" value="Submit">
</form>
<p>These are the values of each element in the form:</p>
<p id="demo"></p>
<script>
const x = document.forms["frm1"];
let text = "";
for (let i = 0; i < x.length ;i++) {
text += x.elements[i].value + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
JavaScript Output
<html>
<head>
<title>Java</title>
</head>
<body>
<p id="demo"> </p>
<script>
document.getElementById("demo").innerHTML=5+6;
alert("I am alerting. You got this?");
console.log("Hi");
</script>
<button type="button" onclick="document.write('The write property');">Click
here</button>
</body>
</html>
JavaScript Statements
• JavaScript statements are composed of: Values, Operators,
Expressions, Keywords, and Comments.
• The statements are executed, one by one, in the same order as they
are written.
• Semicolons separate JavaScript statements.
• JavaScript ignores multiple spaces.
• JavaScript statements can be grouped together in code blocks, inside
curly brackets {...}.
• JavaScript statements often start with a keyword to identify the
JavaScript action to be performed.
Keywor Description
d
var Declares a variable
let Declares a block variable
const Declares a block constant
if Marks a block of statements to be executed on a condition
switch Marks a block of statements to be executed in different cases
for Marks a block of statements to be executed in a loop
function Declares a function
return Exits a function
try Implements error handling to a block of statements
JavaScript Syntax
• JavaScript syntax is the set of rules, how JavaScript programs are
constructed.
• The JavaScript syntax defines two types of values: Fixed values and
Variable values.
• Fixed values
• Fixed values are called Literals.
• The two most important syntax rules for fixed values are:
• Numbers that are written with or without decimals and Strings that are text, written within
double or single quotes.
• Variable values
• Variable values are called Variables.
• In a programming language, variables are used to store data values.
• JavaScript uses the keywords var, let and const to declare variables.
• An equal sign is used to assign values to variables.
JavaScript Comments
• JavaScript comments can be used to explain JavaScript code, and to
make it more readable.
• JavaScript comments can also be used to prevent execution, when
testing alternative code.
• In Single Line Comments, that start with //, Any text between // and the end of the
line will be ignored by JavaScript (will not be executed).
• Example
// Change heading
• Multi-line comments start with /* and end with */. Any text between /* and */ will be
ignored by JavaScript.
• Example
/* The code below will change the heading with id = "myH"
and the paragraph with id = "myP" in my web page: */
JavaScript Variables
• There are 4 Ways to Declare a JavaScript Variable: Using var, Using let, using
const and using nothing.
• The let and const keywords were added to JavaScript in 2015. If you want your code
to run in older browsers, you must use var.
• If you want a general rule: always declare variables with const.
• If you think the value of the variable can change, use let.
• Examples:
• var x = 5;
• let x = 5;
• const price1 = 5;
const price2 = 6;
let total = price1 + price2;
• All JavaScript variables must be identified with unique names. These unique names
are called identifiers.
• Identifiers are JavaScript names used to name variables,keywords, and functions.
• The rules for legal names are the same in most programming languages.
• A JavaScript name must begin with: A letter (A-Z or a-z) , A dollar sign ($) Or an
underscore (_)
• Subsequent characters may be letters, digits, underscores, or dollar signs.
• Numbers are not allowed as the first character in names.
• All JavaScript identifiers are case sensitive.
• Creating a variable in JavaScript is called "declaring" a variable.
• After the declaration, the variable has no value (technically it is undefined).
• To assign a value to the variable, use the equal sign.
• You can also assign a value to the variable when you declare it.
• You can declare many variables in one statement by separating the variables
by comma.
• If you re-declare a JavaScript variable declared with var, it will not lose its
value. But you cannot re-declare a variable declared with let or const.
• Variables declared inside a { } block cannot be accessed from outside the
<p id="demo"></p>
<p id="demo1"></p>
<script>
{ var x=5; // can be accessed outside this block. }
{ let x=2; // cannot be accessed outside this block. }
y=6; // y variable hoisted above the declaration
var y;
document.getElementById("demo").innerHTML="x="+x+" and
"+"y="+y;
//**************************************************************
const z = 3;
z = 3.14;
z = z + 10;
document.getElementById("demo1").innerHTML=z;
//**************************************************************
</script>
JavaScript Operators
• There are different types of JavaScript operators:
• Arithmetic Operators
• Arithmetic Operators are used to perform arithmetic on numbers.
• Assignment Operators
• Assignment operators assign values to JavaScript variables.
• Comparison Operators
• Operator Description
• == equal to
• === equal value and equal type
• != not equal
• !== not equal value or not equal type
• > greater than
• < less than
• >= greater than or equal to
• <= less than or equal to
• ? ternary operator
• String Operators
• All the comparison operators above can also be used on strings alphabetically. The + can also be used to
add (concatenate) strings. Adding a number and a string will return a string.
• Logical Operators8
• Operator Description
• && logical and
• || logical or
• ! logical not
<p id="op"></p>
<p id="st"></p>
<script>
let x = 5, y=10;
let str1='Res', str2="ult=";
let female=10, male=30;
//total = female + male + " students";
total = "students=" + female + male; // evaluating expressions left
to right
str3=str1+str2; //Concatenation
// z = x + y; //Arithmetic Operator
// z=x<=y; // Comparision operator
// z = (x<y ? x : y); // ternary operator
z= !((x<y) && (y>x)); //Logical operator
document.getElementById("op").innerHTML = str3+z;
document.getElementById("st").innerHTML = total;
• JavaScript arrays are written with square brackets.
• Array items are separated by commas.
• The following code declares (creates) an array called cars, containing three items (car
names):
const cars = ["Saab", "Volvo", "BMW"];
• Array indexes are zero-based, which means the first item is [0], second is [1], and so
on.
• JavaScript objects are written with curly braces {}.
• Object properties are written as name:value pairs, separated by commas.
const person = {firstName:"John", lastName:"Doe", age:50,
eyeColor:"blue"};
• The object (person) in the example above has 4 properties: firstName, lastName, age,
and eyeColor.
• You can use the JavaScript typeof operator to find the type of a JavaScript
variable.
• The typeof operator returns the type of a variable or an expression:
typeof "" // Returns "string“
<p id="dtype"></p>
<script>
const person = {firstName:"John", lastName:"Doe"};
document.getElementById("dtype").innerHTML=typeof person;
</script>
JavaScript Functions
• A JavaScript function is a block of code designed to perform a particular task.
• A JavaScript function is executed when "something" invokes it (calls it).
• A JavaScript function is defined with the function keyword, followed by a name,
followed by parentheses ().
• Function names can contain letters, digits, underscores, and dollar signs (same rules
as variables).
• The parentheses may include parameter names separated by commas: (parameter1,
parameter2, ...).
• The code to be executed, by the function, is placed inside curly brackets: {}.
• function name(parameter1, parameter2, parameter3…) {
// code to be executed
}
• Function parameters are listed inside the parentheses () in the function definition.
• Function arguments are the values received by the function when it is invoked.
• Inside the function, the arguments (the parameters) behave as local variables.
• The code inside the function will execute when "something" invokes (calls) the
function:
• When an event occurs (when a user clicks a button)
• When it is invoked (called) from JavaScript code
• Automatically (self invoked)
<p id="func"></p>
<script>
x=add(4,5);
document.getElementById("func").innerHTML =
x;
function add(a,b) { return a+b; }
</script>
<?php
// Create connection
$conn = new mysqli("localhost", "root", " "); //server name, username and
password
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
o The connection will be closed automatically when the script ends. To close the connection
before, use the following:
� $conn->close(); // MySQLi Object-Oriented:
• <?php
// Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>
o The CREATE TABLE statement is used to create a table in MySQL.