Overview of Web Technology Essentials
Overview of Web Technology Essentials
Web Technology refers to the various tools and techniques that are utilized in the process of
communication between different types of devices over the Internet. A web browser is used to
access web pages. Web browsers can be defined as programs that display text, data, pictures,
animation, and video on the Internet. Hyperlinked resources on the World Wide Web can be
accessed using software interfaces provided by Web browsers.
• World Wide Web (WWW): The World Wide Web is based on several different
technologies: Web browsers, Hypertext Markup Language (HTML), and
Hypertext Transfer Protocol (HTTP).
• Web Browser: The web browser is an application software to explore www
(World Wide Web). It provides an interface between the server and the client and
requests to the server for web documents and services.
• Web Server: Web server is a program which processes the network requests of
the users and serves them with files that create web pages. This exchange takes
place using Hypertext Transfer Protocol (HTTP).
• Web Pages: A webpage is a digital document that is linked to the World Wide
Web and viewable by anyone connected to the internet has a web browser.
• Web Development: Web development refers to the building, creating, and
maintaining of websites. It includes aspects such as web design, web publishing,
web programming, and database management. It is the creation of an application
that works over the internet i.e., websites.
• Frontend Development: The part of a website that the user interacts directly is
termed as front end. It is also referred to as the ‘client side’ of the application.
• Backend Development: Backend is the server side of a website. It is the part of
the website that users cannot see and interact. It is the portion of software that
does not come in direct contact with the users. It is used to store and arrange
data.
Frontend Languages: The front-end portion is built by using some languages which are
discussed below:
• HTML: HTML stands for Hypertext Markup Language. It is used to design the
front-end portion of web pages using a markup language. HTML is the
combination of Hypertext and Markup language. Hypertext defines the link
between the web pages. The markup language is used to define the text
documentation within the tag which defines the structure of web pages.
• CSS: Cascading Style Sheets fondly referred to as CSS is a simply designed
language intended to simplify the process of making web pages presentable.
CSS allows you to apply styles to web pages. More importantly, CSS enables
you to do this independent of the HTML that makes up each web page.
• JavaScript: JavaScript is a famous scripting language used to create magic on
the sites to make the site interactive for the user. It is used to enhancing the
functionality of a website to running cool games and web-based software.
• AJAX: Ajax is an acronym for Asynchronous Javascript and XML. It is used to
communicate with the server without refreshing the web page and thus
increasing the user experience and better performance.
There are many other languages through which one can do front-end development depending
upon the framework for
example Flutter user Dart, react uses JavaScript and Django uses Python, and much more.
A "Brief" History of the Web
While working for CERN in 1989 a British scientist of the name Tim
Berners-Lee invented the World Wide Web. The idea was to create a
world-wide network of computers to form a global information
system.
It wouldn't be until late 1990 where Berners-Lee deployed
the world's first web page. A web page is a text document written in
a special formatting language called the Hypertext Mark-up
Language (HTML).
Hyper Text: HyperText simply means "Text within Text." A text has a link within it, is a
hypertext. Whenever you click on a link which brings you to a new webpage, you have
clicked on a hypertext. HyperText is a way to link two or more web pages (HTML
documents) with each other.
Web Page: A web page is a document which is commonly written in HTML and
translated by a web browser. A web page can be identified by entering an URL. A Web
page can be of the static or dynamic type. With the help of HTML only, we can
create static web pages.
Hence, HTML is a markup language which is used for creating attractive web pages
with the help of styling, and which looks in a nice format on a web browser. An HTML
document is made of many HTML tags and each HTML tag contains different content.
1. <!DOCTYPE>
2. <html>
3. <head>
4. <title>Web page title</title>
5. </head>
6. <body>
7. <h1>Write Your First Heading</h1>
8. <p>Write Your First Paragraph.</p>
9. </body>
10. </html>
Test it Now
<html > :This tag informs the browser that it is an HTML document. Text between
html tag describes the web document. It is a container for all other elements of HTML
except <!DOCTYPE>
<head>: It should be the first element inside the <html> element, which contains the
metadata(information about the document). It must be closed before the body tag
opens.
<title>: As its name suggested, it is used to add title of that HTML page which appears
at the top of the browser window. It must be placed inside the head tag and should
close immediately. (Optional)
<body> : Text between body tag describes the body content of the page that is visible
to the end user. This tag contains the main content of the HTML document.
<h1> : Text between <h1> tag describes the first level heading of the webpage.
<p> : Text between <p> tag describes the paragraph of the webpage.
Tim Berners-Lee is known as the father of HTML. The first available description of
HTML was a document called "HTML Tags" proposed by Tim in late 1991. The latest
version of HTML is HTML5, which we will learn later in this tutorial.
HTML Versions
Since the time HTML was invented there are lots of HTML versions in market, the brief
introduction about the HTML version is given below:
HTML 1.0: The first version of HTML was 1.0, which was the barebones version of
HTML language, and it was released in1991.
HTML 2.0: This was the next version which was released in 1995, and it was standard
language version for website design. HTML 2.0 was able to support extra features such
as form-based file upload, form elements such as text box, option button, etc.
HTML 3.2: HTML 3.2 version was published by W3C in early 1997. This version was
capable of creating tables and providing support for extra options for form elements.
It can also support a web page with complex mathematical equations. It became an
official standard for any browser till January 1997. Today it is practically supported by
most of the browsers.
HTML 4.01: HTML 4.01 version was released on December 1999, and it is a very stable
version of HTML language. This version is the current official standard, and it provides
added support for stylesheets (CSS) and scripting ability for various multimedia
elements.
HTML5 : HTML5 is the newest version of HyperText Markup language. The first draft
of this version was announced in January 2008. There are two major organizations one
is W3C (World Wide Web Consortium), and another one is WHATWG( Web Hypertext
Application Technology Working Group) which are involved in the development of
HTML 5 version, and still, it is under development.
Features of HTML
1) It is a very easy and simple language. It can be easily understood and modified.
2) It is very easy to make an effective presentation with HTML because it has a lot of
formatting tags.
4) It facilitates programmers to add a link on the web pages (by html anchor tag), so
it enhances the interest of browsing of the user.
6) It facilitates the programmer to add Graphics, Videos, and Sound to the web pages
which makes it more attractive and interactive.
7) HTML is a case-insensitive language, which means we can use tags either in lower-
case or upper-case
HTML Comments
Comments are some text or code written in your code to give an explanation about
the code, and not visible to the user. Comments which are used for HTML file are
known as HTML comments. Anything written between these tags will be ignored by
the browser, so comments will not be visible on the webpage.
Comments of any code make code easy to understand and increase readability of
code.
Comments are also part of the code, which gives an explanation of the code.
Syntax
1. <! -- Write commented text here -->
Such as:
Multiline Comment
In HTML code, we can also comments multiple lines at a time. In multiline comment
we can use any description about code or multiple line code to debug, etc.
Syntax
1. <!---
2. Your code is commented.
3. Write description of code.
4. It will not display at webpage.
5. -->
Example:
1. <h2>Cake Gallery</h2>
2. <!-- This is image for a yummy cake
3. you can see it on your web-page
4. of your favorite browser -->
5. <img src="[Link] alt="cak
e image" height="300px"
6. width="300px">
HTML Form
An HTML form is a section of a document which contains controls such as text fields,
password fields, checkboxes, radio buttons, submit button, menus etc.
An HTML form facilitates the user to enter data that is to be sent to the server for
processing such as name, email address, password, phone number, etc. .
For example: If a user want to purchase some items on internet, he/she must fill the
form such as shipping address and credit/debit card details so that item can be sent
to the given address.
Tag Description
Tag Description
1. <form action="#">
2. <table>
3. <tr>
4. <td class="tdLabel"><label for="register_name" class="label">Enter name:
</label></td>
5. <td><input type="text" name="name" value="" id="register_name" style=
"width:160px"/></td>
6. </tr>
7. <tr>
8. <td class="tdLabel"><label for="register_password" class="label">Enter p
assword:</label></td>
9. <td><input type="password" name="password" id="register_password" st
yle="width:160px"/></td>
10. </tr>
11. <tr>
12. <td class="tdLabel"><label for="register_email" class="label">Enter Email:
</label></td>
13. <td
14. ><input type="email" name="email" value="" id="register_email" style="widt
h:160px"/></td>
15. </tr>
16. <tr>
17. <td class="tdLabel"><label for="register_gender" class="label">Enter Gen
der:</label></td>
18. <td>
19. <input type="radio" name="gender" id="register_gendermale" value="male"
/>
20. <label for="register_gendermale">male</label>
21. <input type="radio" name="gender" id="register_genderfemale" value="fem
ale"/>
22. <label for="register_genderfemale">female</label>
23. </td>
24. </tr>
25. <tr>
26. <td class="tdLabel"><label for="register_country" class="label">Select Co
untry:</label></td>
27. <td><select name="country" id="register_country" style="width:160px">
28. <option value="india">india</option>
29. <option value="pakistan">pakistan</option>
30. <option value="africa">africa</option>
31. <option value="china">china</option>
32. <option value="other">other</option>
33. </select>
34. </td>
35. </tr>
36. <tr>
37. <td colspan="2"><div align="right"><input type="submit" id="register_0
" value="register"/>
38. </div></td>
39. </tr>
40. </table>
41. </form>
HTML Formatting
HTML Formatting is a process of formatting text for better look and feel. HTML
provides us ability to format text without using CSS. There are many formatting tags
in HTML. These tags are used to make text bold, italicized, or underlined. There are
almost 14 options available that how text appears in HTML and XHTML.
o Physical tag: These tags are used to provide the visual appearance to the text.
o Logical tag: These tags are used to add some logical or semantic value to the text.
NOTE: There are some physical and logical tags which may give same visual
appearance, but they will be different in semantics.
Here, we are going to learn 14 HTML formatting tags. Following is the list of HTML
formatting text.
Element Description
name
<b> This is a physical tag, which is used to bold the text written between it.
<strong> This is a logical tag, which tells the browser that the text is important.
<tt> This tag is used to appear a text in teletype. (not supported in HTML5)
<strike> This tag is used to draw a strikethrough on a section of text. (Not supported in
HTML5)
<sup> It displays the content slightly above the normal line.
<big> This tag is used to increase the font size by one conventional unit.
<small> This tag is used to decrease the font size by one unit from base font size.
1) Bold Text
HTML<b> and <strong> formatting elements
The HTML <b> element is a physical tag which display text in bold font, without any
logical importance. If you write anything within <b>............</b> element, is shown in
bold letters.
Output:
The HTML <strong> tag is a logical tag, which displays the content in bold font and
informs the browser about its logical importance. If you write anything between
<strong>???????. </strong>, is shown important text.
Output:
2) Italic Text
HTML <i> and <em> formatting elements
The HTML <i> element is physical element, which display the enclosed content in italic
font, without any added importance. If you write anything within <i>............</i>
element, is shown in italic letters.
Output:
The HTML <em> tag is a logical element, which will display the enclosed content in
italic font, with added semantics importance.
Output:
Output:
4) Underlined Text
If you write anything within <u>.........</u> element, is shown in underlined text.
Output:
Output:
6) Monospaced Font
If you want that each letter has the same width then you should write the content
within <tt>.............</tt> element.
Note: We know that most of the fonts are known as variable-width fonts because
different letters have different width. (for example: 'w' is wider than 'i'). Monospaced
Font provides similar space among every letter.
Output:
7) Superscript Text
If you put the content within <sup>..............</sup> element, is shown in superscript;
means it is displayed half a character's height above the other characters.
8) Subscript Text
If you put the content within <sub>..............</sub> element, is shown in subscript ;
means it is displayed half a character's height below the other characters.
Output:
9) Deleted Text
Anything that puts within <del>..........</del> is displayed as deleted text.
Output:
Hello
Output:
Delete your first [Link] another paragraph.
Output:
A <frame> tag is used with <frameset>, and it divides a webpage into multiple
sections or frames, and each frame can contain different web pages.
Syntax
1. < frame src = "URL" >
Display Block
Usage Frames
Example 1
Create Vertical frames:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>Frame tag</title>
5. </head>
6. <frameset cols="25%,50%,25%">
7. <frame src="[Link]" >
8. <frame src="[Link]">
9. <frame src="[Link]">
10. </frameset>
11. </html>
Example 2:
Create Horizontal frames:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>Frame tag</title>
5. </head>
6. <frameset rows="30%, 40%, 30%">
7. <frame name="top" src="[Link]" >
8. <frame name="main" src="[Link]">
9. <frame name="bottom" src="[Link]">
10. </frameset>
11. </html>
HTML Links
What is a link?
It is a connection from one web resource to another. A link has two ends, An
anchor and direction. The link starts at the “source” anchor and points to the
“destination” anchor, which may be any Web resource such as an image, a
video clip, a sound bite, a program, an HTML document or an element
within an HTML document. You will find many websites or social media
platforms ( Like YouTube, and Instagram ) which link an image to a URL or a
text to a URL etc.
This basically means that by using the ‘a’ tag, you can link 1 element of the
code to another element that may/may not be in your code.
HTML Link Syntax
Links are specified in HTML using the “a” tag.
Syntax Explanation:
href : The href attribute is used to specify the destination
address
of the link used. "href" stands for Hypertext reference.
Text link : The text link is the visible part of the link.
It is what the viewer clicks on.
Input :
• html
<!DOCTYPE html>
<html>
<h3>Example Of Adding a link</h3>
<body>
<p>Click on the following link</p>
<a href="[Link]
</body>
</html>
Output :
Internal Links
An internal link is a type of hyperlink whose target or destination is a
resource, such as an image or document, on the same website or domain.
Input:
• html
<!DOCTYPE html>
<html>
<h3>Internal Link And External Link Example</h3>
<body>
<!--internal link-->
<p>
<a href="html_contribute.asp/">
GeeksforGeeks Contribute
</a>
It is a link to the contribute page on GeeksforGeeks' website.
</p>
<!--external link-->
<p>
<a href="[Link]
GeeksforGeeks
</a>
It is a link to the GeeksforGeeks website on the World Wide Web.
</p>
</body>
</html>
Output :
<!DOCTYPE html>
<html>
<head>
<style>
a:link {
color: red;
background-color: transparent;
}
a:visited {
color: green;
background-color: transparent;
}
a:hover {
color: blue;
background-color: transparent;
}
a:active {
color: yellow;
background-color: transparent;
}
</style>
</head>
<body>
<p>Changing the default colors of links</p>
<p>Visited Link</p>
<a href="[Link]
GeeksforGeeks
</a>
<p>Link</p>
<a href="[Link]
facebook
</a>
<p>hovering effect</p>
<a href="[Link]
GeeksforGeeks
</a>
</body>
</html>
Output:
Input:
• html
<!DOCTYPE html>
<html>
<body>
<h3>Various options available in the Target Attribute</h3>
</html>
Output:
<!DOCTYPE html>
<html>
<body>
<h3>Using Image as a link</h3>
<p>Click on the image to visit GeeksforGeeks homepage.</p>
<a href="[Link]
<img src="[Link]
alt="GeeksforGeeks"
style="width:80px;height:80px;border:0">
</a>
</body>
</html>
Output:
Input :
• html
<!DOCTYPE html>
<html>
<body>
<h2>Topic 1</h2>
<p>paragraph 1
.....</p>
<h2>Topic 2</h2>
<p>paragraph 1
.....</p>
<h2>Topic 3</h2>
<p>paragraph 1
.....</p>
<h2>Topic 4</h2>
<p>paragraph 1
.....</p>
<h2>Topic 5</h2>
<p>paragraph 1
.....</p>
<h2>Topic 6</h2>
<p>paragraph 1
.....</p>
<h2>Topic 7</h2>
<p>paragraph 1
.....</p>
<h2>Topic 8</h2>
<p>paragraph 1
.....</p>
<h2>Topic 9</h2>
<p>paragraph 1
.....</p>
<h2>Topic 10</h2>
<p>paragraph 1
.....</p>
<h2 id="T11">Topic 11</h2>
<p>paragraph 1
.....</p>
<h2>Topic 12</h2>
<p>paragraph 1
.....</p>
<h2>Topic 13</h2>
<p>paragraph 1
.....</p>
<h2>Topic 14</h2>
<p>paragraph 1
.....</p>
<h2>Topic 15</h2>
<p>paragraph 1
.....</p>
<h2>Topic 16</h2>
<p>paragraph 1
.....</p>
<h2>Topic 18</h2>
<p>paragraph 1
.....</p>
<h2>Topic 19</h2>
<p>paragraph 1
.....</p>
</html>
Output:
Creating a download link in HTML
A text link of a pdf, doc or zip file can be created to make it downloadable.
Input :
• html
<!DOCTYPE html>
<html>
<body>
<a href="[Link]">
Download PDF File
</a>
</body>
</html>
HTML Lists
HTML Lists are used to specify lists of information. All lists may contain one or more
list elements. There are three different types of HTML lists:
1. <ol>
2. <li>Aries</li>
3. <li>Bingo</li>
4. <li>Leo</li>
5. <li>Oracle</li>
6. </ol>
1. <ul>
2. <li>Aries</li>
3. <li>Bingo</li>
4. <li>Leo</li>
5. <li>Oracle</li>
6. </ul>
The definition list is very appropriate when you want to present glossary, list of terms
or other name-value list.
1. <dl>
2. <dt>Aries</dt>
3. <dd>-One of the 12 horoscope sign.</dd>
4. <dt>Bingo</dt>
5. <dd>-One of my evening snacks</dd>
6. <dt>Leo</dt>
7. <dd>-It is also an one of the 12 horoscope sign.</dd>
8. <dt>Oracle</dt>
9. <dd>-It is a multinational technology corporation.</dd>
10. </dl>
Code:
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>Nested list</title>
5. </head>
6. <body>
7. <p>List of Indian States with thier capital</p>
8. <ol>
9. <li>Delhi
10. <ul>
11. <li>NewDelhi</li>
12. </ul>
13. </li>
14. <li>Haryana
15. <ul>
16. <li>Chandigarh</li>
17. </ul>
18. </li>
19. <li>Gujarat
20. <ul>
21. <li>Gandhinagar</li>
22. </ul>
23. </li>
24. <li>Rajasthan
25. <ul>
26. <li>Jaipur</li>
27. </ul>
28. </li>
29. <li>Maharashtra
30. <ul>
31. <li>Mumbai</li>
32. </ul>
33. </li>
34. <li>Uttarpradesh
35. <ul>
36. <li>Lucknow</li></ul>
37. </li>
38. </ol>
39. </body>
40. </html>
HTML Table
HTML table tag is used to display data in tabular form (row * column). There can be
many columns in a row.
We can create a table to display data in tabular form, using <table> element, with the
help of <tr> , <td>, and <th> elements.
In Each table, table row is defined by <tr> tag, table header is defined by <th>, and
table data is defined by <td> tags.
HTML tables are used to manage the layout of the page e.g. header section, navigation
bar, body content, footer section etc. But it is recommended to use div tag over table
to manage the layout of the page .
Tag Description
<col> It is used with <colgroup> element to specify column properties for each
column.
1. <table>
2. <tr><th>First_Name</th><th>Last_Name</th><th>Marks</th></tr>
3. <tr><td>Sonoo</td><td>Jaiswal</td><td>60</td></tr>
4. <tr><td>James</td><td>William</td><td>80</td></tr>
5. <tr><td>Swati</td><td>Sironi</td><td>82</td></tr>
6. <tr><td>Chetna</td><td>Singh</td><td>72</td></tr>
7. </table>
Introduction of JavaScript
Our JavaScript Tutorial is designed for beginners and professionals both. JavaScript
is used to create client-side dynamic pages.
What is JavaScript
JavaScript (js) is a light-weight object-oriented programming language which is used
by several websites for scripting the webpages. It is an interpreted, full-fledged
programming language that enables dynamic interactivity on websites when applied
to an HTML document. It was introduced in the year 1995 for adding programs to the
webpages in the Netscape Navigator browser. Since then, it has been adopted by all
other graphical web browsers. With JavaScript, users can build modern web
applications to interact directly without reloading the page every time. The traditional
website uses js to provide several forms of interactivity and simplicity.
Although, JavaScript has no connectivity with Java programming language. The name
was suggested and provided in the times when Java was gaining popularity in the
market. In addition to web browsers, databases such as CouchDB and MongoDB uses
JavaScript as their scripting and query language.
Features of JavaScript
There are following features of JavaScript:
1. All popular web browsers support JavaScript as they provide built-in execution
environments.
2. JavaScript follows the syntax and structure of the C programming language.
Thus, it is a structured programming language.
3. JavaScript is a weakly typed language, where certain types are implicitly cast
(depending on the operation).
4. JavaScript is an object-oriented programming language that uses prototypes
rather than using classes for inheritance.
5. It is a light-weighted and interpreted language.
6. It is a case-sensitive language.
7. JavaScript is supportable in several operating systems including, Windows,
macOS, etc.
8. It provides good control to the users over the web browsers.
History of JavaScript
In 1993, Mosaic, the first popular web browser, came into existence. In the year
1994, Netscape was founded by Marc Andreessen. He realized that the web needed
to become more dynamic. Thus, a 'glue language' was believed to be provided to
HTML to make web designing easy for designers and part-time programmers.
Consequently, in 1995, the company recruited Brendan Eich intending to implement
and embed Scheme programming language to the browser. But, before Brendan could
start, the company merged with Sun Microsystems for adding Java into its Navigator
so that it could compete with Microsoft over the web technologies and platforms.
Now, two languages were there: Java and the scripting language. Further, Netscape
decided to give a similar name to the scripting language as Java's. It led to 'Javascript'.
Finally, in May 1995, Marc Andreessen coined the first code of Javascript named
'Mocha'. Later, the marketing team replaced the name with 'LiveScript'. But, due to
trademark reasons and certain other reasons, in December 1995, the language was
finally renamed to 'JavaScript'. From then, JavaScript came into existence.
Application of JavaScript
JavaScript is used to create interactive websites. It is mainly used for:
o Client-side validation,
o Dynamic drop-down menus,
o Displaying date and time,
o Displaying pop-up windows and dialog boxes (like an alert dialog box, confirm
dialog box and prompt dialog box),
o Displaying clocks etc.
JavaScript Example
1. <script>
2. [Link]("Hello JavaScript by JavaScript");
3. </script>
JavaScript addEventListener()
The addEventListener() method is used to attach an event handler to a particular
element. It does not override the existing event handlers. Events are said to be an
essential part of the JavaScript. A web page responds according to the event that
occurred. Events can be user-generated or generated by API's. An event listener is a
JavaScript's procedure that waits for the occurrence of an event.
Syntax
Although it has three parameters, the parameters event and function are widely used.
The third parameter is optional to define. The values of this function are defined as
follows.
Parameter Values
event: It is a required parameter. It can be defined as a string that specifies the event's
name.
Note: Do not use any prefix such as "on" with the parameter value. For example,
Use "click" instead of using "onclick".
Example
It is a simple example of using the addEventListener() method. We have to click the
given HTML button to see the effect.
1. <!DOCTYPE html>
2. <html>
3. <body>
4. <p> Example of the addEventListener() method. </p>
5. <p> Click the following button to see the effect. </p>
6. <button id = "btn"> Click me </button>
7. <p id = "para"></p>
8. <script>
9. [Link]("btn").addEventListener("click", fun);
10. function fun() {
11. [Link]("para").innerHTML = "Hello World" + "<br>" + "W
elcome to the [Link]";
12. }
13. </script>
14. </body>
15. </html>
Test it Now
Output
Now, in the next example we will see how to add many events to the same element
without overwriting the existing events.
Example
In this example, we are adding multiple events to the same element.
1. <!DOCTYPE html>
2. <html>
3. <body>
4. <p> This is an example of adding multiple events to the same element. </p>
5. <p> Click the following button to see the effect. </p>
6. <button id = "btn"> Click me </button>
7. <p id = "para"></p>
8. <p id = "para1"></p>
9. <script>
10. function fun() {
11. alert("Welcome to the [Link]");
12. }
13.
14. function fun1() {
15. [Link]("para").innerHTML = "This is second function";
16.
17. }
18. function fun2() {
19. [Link]("para1").innerHTML = "This is third function";
20. }
21. var mybtn = [Link]("btn");
22. [Link]("click", fun);
23. [Link]("click", fun1);
24. [Link]("click", fun2);
25. </script>
26. </body>
27. </html>
Test it Now
Output
Now, when we click the button, an alert will be displayed. After clicking the given HTML
button, the output will be -
Example
In this example, we are adding multiple events of a different type to the same element.
1. <!DOCTYPE html>
2. <html>
3. <body>
4. <p> This is an example of adding multiple events of different type to the same eleme
nt. </p>
5. <p> Click the following button to see the effect. </p>
6. <button id = "btn"> Click me </button>
7. <p id = "para"></p>
8. <script>
9. function fun() {
10. [Link] = "50px";
11. [Link] = "50px";
12. [Link] = "yellow";
13. [Link] = "blue";
14. }
15.
16. function fun1() {
17. [Link]("para").innerHTML = "This is second function";
18.
19. }
20. function fun2() {
21. [Link] = "";
22. [Link] = "";
23. [Link] = "";
24. [Link] = "";
25. }
26. var mybtn = [Link]("btn");
27. [Link]("mouseover", fun);
28. [Link]("click", fun1);
29. [Link]("mouseout", fun2);
30. </script>
31. </body>
32. </html>
Test it Now
Output
When we move the cursor over the button, the output will be -
After clicking the button and leave the cursor, the output will be -
In HTML DOM, Bubbling and Capturing are the two ways of event propagation. We
can understand these ways by taking an example.
Suppose we have a div element and a paragraph element inside it, and we are applying
the "click" event to both of them using the addEventListener() method. Now the
question is on clicking the paragraph element, which element's click event is handled
first.
So, in Bubbling, the event of paragraph element is handled first, and then the div
element's event is handled. It means that in bubbling, the inner element's event is
handled first, and then the outermost element's event will be handled.
In Capturing the event of div element is handled first, and then the paragraph
element's event is handled. It means that in capturing the outer element's event is
handled first, and then the innermost element's event will be handled.
We can specify the propagation using the useCapture parameter. When it is set to
false (which is its default value), then the event uses bubbling propagation, and when
it is set to true, there is the capturing propagation.
Example
In this example, there are two div elements. We can see the bubbling effect on the first
div element and the capturing effect on the second div element.
When we double click the span element of the first div element, then the span
element's event is handled first than the div element. It is called bubbling.
But when we double click the span element of the second div element, then the div
element's event is handled first than the span element. It is called capturing.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. div{
6. background-color: lightblue;
7. border: 2px solid red;
8. font-size: 25px;
9. text-align: center;
10. }
11. span{
12. border: 2px solid blue;
13. }
14. </style>
15. </head>
16. <body>
17. <h1> Bubbling </h1>
18. <div id = "d1">
19. This is a div element.
20. <br><br>
21. <span id = "s1"> This is a span element. </span>
22. </div>
23. <h1> Capturing </h1>
24. <div id = "d2"> This is a div element.
25. <br><br>
26. <span id = "s2"> This is a span element. </span>
27. </div>
28.
29. <script>
30. [Link]("d1").addEventListener("dblclick", function() {alert('You hav
e double clicked on div element')}, false);
31. [Link]("s1").addEventListener("dblclick", function() {alert('Y
ou have double clicked on span element')}, false);
32. [Link]("d2").addEventListener("dblclick", function() {alert('You hav
e double clicked on div element')}, true);
33. [Link]("s2").addEventListener("dblclick", function() {alert('Y
ou have double clicked on span element')}, true);
34. </script>
35. </body>
36. </html>
Test it Now
Output
We have to double click the specific elements to see the effect.
JavaScript Array
JavaScript array is an object that represents a collection of similar type of elements.
1. By array literal
2. By creating instance of Array directly (using new keyword)
3. By using an Array constructor (using new keyword)
1. var arrayname=[value1,value2.....valueN];
As you can see, values are contained inside [ ] and separated by , (comma).
Let's see the simple example of creating and using array in JavaScript.
1. <script>
2. var emp=["Sonoo","Vimal","Ratan"];
3. for (i=0;i<[Link];i++){
4. [Link](emp[i] + "<br/>");
5. }
6. </script>
Sonoo
Vimal
Ratan
1. <script>
2. var i;
3. var emp = new Array();
4. emp[0] = "Arun";
5. emp[1] = "Varun";
6. emp[2] = "John";
7.
8. for (i=0;i<[Link];i++){
9. [Link](emp[i] + "<br>");
10. }
11. </script>
Arun
Varun
John
1. <script>
2. var emp=new Array("Jai","Vijay","Smith");
3. for (i=0;i<[Link];i++){
4. [Link](emp[i] + "<br>");
5. }
6. </script>
Jai
Vijay
Smith
JavaScript Array Methods
Let's see the list of JavaScript array methods with their description.
Methods Description
concat() It returns a new array object that contains two or more merged
arrays.
copywithin() It copies the part of the given array with its own elements and
returns the modified array.
entries() It creates an iterator object and a loop that iterates over each
key/value pair.
flatMap() It maps all array elements via mapping function, then flattens
the result into a new array.
from() It creates a new array carrying the exact copy of another array
element.
filter() It returns the new array containing the elements that pass the
provided function conditions.
find() It returns the value of the first element in the given array that
satisfies the specified condition.
findIndex() It returns the index value of the first element in the given array
that satisfies the specified condition.
keys() It creates an iterator object that contains only the keys of the
array, then loops through these keys.
lastIndexOf() It searches the specified element in the given array and returns
the index of the last match.
map() It calls the specified function for every array element and returns
the new array
reduce(function, It executes a provided function for each value from left to right
initial) and reduces the array to a single value.
reduceRight() It executes a provided function for each value from right to left
and reduces the array to a single value.
some() It determines if any element of the array passes the test of the
implemented function.
slice() It returns a new array containing the copy of the part of the
given array.
values() It creates a new iterator object carrying values for each index in
the array.
JavaScript Events
The change in the state of an object is known as an Event. In html, there are various
events which represents that some activity is performed by the user or by the browser.
When javascript code is included in HTML, js react over these events and allow the
execution. This process of reacting over the events is called Event Handling. Thus, js
handles the HTML events via Event Handlers.
For example, when a user clicks over the browser, add js code, which will execute the
task to be performed on the event.
Mouse events:
mouseover onmouseover When the cursor of the mouse comes over the
element
Keyboard events:
Keydown & onkeydown & When the user press and then release
Keyup onkeyup the key
Form events:
Event Performed Event Handler Description
Window/Document events
load onload When the browser finishes the loading of the page
unload onunload When the visitor leaves the current webpage, the
browser unloads it
resize onresize When the visitor resizes the window of the browser
Click Event
1. <html>
2. <head> Javascript Events </head>
3. <body>
4. <script language="Javascript" type="text/Javascript">
5. <!--
6. function clickevent()
7. {
8. [Link]("This is JavaTpoint");
9. }
10. //-->
11. </script>
12. <form>
13. <input type="button" onclick="clickevent()" value="Who's this?"/>
14. </form>
15. </body>
16. </html>
MouseOver Event
1. <html>
2. <head>
3. <h1> Javascript Events </h1>
4. </head>
5. <body>
6. <script language="Javascript" type="text/Javascript">
7. <!--
8. function mouseoverevent()
9. {
10. alert("This is JavaTpoint");
11. }
12. //-->
13. </script>
14. <p onmouseover="mouseoverevent()"> Keep cursor over me</p>
15. </body>
16. </html>
Focus Event
1. <html>
2. <head> Javascript Events</head>
3. <body>
4. <h2> Enter something here</h2>
5. <input type="text" id="input1" onfocus="focusevent()"/>
6. <script>
7. <!--
8. function focusevent()
9. {
10. [Link]("input1").[Link]=" aqua";
11. }
12. //-->
13. </script>
14. </body>
Keydown Event
1. <html>
2. <head> Javascript Events</head>
3. <body>
4. <h2> Enter something here</h2>
5. <input type="text" id="input1" onkeydown="keydownevent()"/>
6. <script>
7. <!--
8. function keydownevent()
9. {
10. [Link]("input1");
11. alert("Pressed a key");
12. }
13. //-->
14. </script>
15. </body>
16. </html>
Load event
1. <html>
2. <head>Javascript Events</head>
3. </br>
4. <body onload="[Link]('Page successfully loaded');">
5. <script>
6. <!--
7. [Link]("The page is loaded successfully");
8. //-->
9. </script>
10. </body>
11. </html>
JavaScript Form
Forms are the basics of HTML. We use HTML form element in order to create
the JavaScript form. For creating a form, we can use the following sample code:
1. <html>
2. <head>
3. <title> Login Form</title>
4. </head>
5. <body>
6. <h3> LOGIN </h3>
7. <formform ="Login_form" onsubmit="submit_form()">
8. <h4> USERNAME</h4>
9. <input type="text" placeholder="Enter your email id"/>
10. <h4> PASSWORD</h4>
11. <input type="password" placeholder="Enter your password"/></br></br>
12. <input type="submit" value="Login"/>
13. <input type="button" value="SignUp" onClick="create()"/>
14. </form>
15. </html>
In the code:
o Form name tag is used to define the name of the form. The name of the form
here is "Login_form". This name will be referenced in the JavaScript form.
o The action tag defines the action, and the browser will take to tackle the form
when it is submitted. Here, we have taken no action.
o The method to take action can be either post or get, which is used when the
form is to be submitted to the server. Both types of methods have their own
properties and rules.
o The input type tag defines the type of inputs we want to create in our form.
Here, we have used input type as 'text', which means we will input values as text
in the textbox.
o Net, we have taken input type as 'password' and the input value will be
password.
o Next, we have taken input type as 'button' where on clicking, we get the value
of the form and get displayed.
Other than action and methods, there are the following useful methods also which are
provided by the HTML Form Element
Referencing forms
Now, we have created the form element using HTML, but we also need to make its
connectivity to JavaScript. For this, we use the getElementById () method that
references the html form element to the JavaScript code.
When we submit the form, the action is taken just before the request is sent to the
server. It allows us to add an event listener that enables us to place various validations
on the form. Finally, the form gets ready with a combination of HTML and JavaScript
code.
Let's collect and use all these to create a Login form and SignUp form and use both.
Login Form
1. <html>
2. <head>
3. <title> Login Form</title>
4. </head>
5. <body>
6. <h3> LOGIN </h3>
7. <formform ="Login_form" onsubmit="submit_form()">
8. <h4> USERNAME</h4>
9. <input type="text" placeholder="Enter your email id"/>
10. <h4> PASSWORD</h4>
11. <input type="password" placeholder="Enter your password"/></br></br>
12. <input type="submit" value="Login"/>
13. <input type="button" value="SignUp" onClick="create()"/>
14. </form>
15. <script type="text/javascript">
16. function submit_form(){
17. alert("Login successfully");
18. }
19. function create(){
20. [Link]="[Link]";
21. }
22. </script>
23. </body>
24. </html>
The output of the above code on clicking on Login button is shown below:
SignUp Form
1. <html>
2. <head>
3. <title> SignUp Page</title>
4. </head>
5. <body align="center" >
6. <h1> CREATE YOUR ACCOUNT</h1>
7. <table cellspacing="2" align="center" cellpadding="8" border="0">
8. <tr><td> Name</td>
9. <td><input type="text" placeholder="Enter your name" id="n1"></td></tr
>
10. <tr><td>Email </td>
11. <td><input type="text" placeholder="Enter your email id" id="e1"></td></
tr>
12. <tr><td> Set Password</td>
13. <td><input type="password" placeholder="Set a password" id="p1"></td>
</tr>
14. <tr><td>Confirm Password</td>
15. <td><input type="password" placeholder="Confirm your password" id="p2"
></td></tr>
16. <tr><td>
17. <input type="submit" value="Create" onClick="create_account()"/>
18. </table>
19. <script type="text/javascript">
20. function create_account(){
21. var n=[Link]("n1").value;
22. var e=[Link]("e1").value;
23. var p=[Link]("p1").value;
24. var cp=[Link]("p2").value;
25. //Code for password validation
26. var letters = /^[A-Za-z]+$/;
27. var email_val = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-
9]{2,4})+$/;
28. //other validations required code
29. if(n==''||e==''||p==''||cp==''){
30. alert("Enter each details correctly");
31. }
32. else if()
33. {
34. alert('Name is incorrect must contain alphabets only');
35. }
36. else if (!email_val.test(e))
37. {
38. alert('Invalid email format please enter valid email id');
39. }
40. else if(p!=cp)
41. {
42. alert("Passwords not matching");
43. }
44. else if([Link]("p1").[Link] > 12)
45. {
46. alert("Password maximum length is 12");
47. }
48. else if([Link]("p1").[Link] < 6)
49. {
50. alert("Password minimum length is 6");
51. }
52. else{
53. alert("Your account has been created successfully... Redirecting to JavaTpoint.c
om");
54. [Link]="[Link]
55. }
56. }
57. </script>
58. </body>
59. </html>
1. <script>
2. function msg(){
3. alert("hello! this is message");
4. }
5. </script>
6. <input type="button" onclick="msg()" value="call function"/>
Output of the above example
1. <script>
2. function getInfo(){
3. return "hello javatpoint! How r u?";
4. }
5. </script>
6. <script>
7. [Link](getInfo());
8. </script>
Output of the above example
hello javatpoint! How r u?
Syntax
1. new Function ([arg1[, arg2[, ....argn]],] functionBody)
Parameter
arg1, arg2, .... , argn - It represents the argument used by function.
Method Description
apply() It is used to call a function contains this value and a single array of arguments.
call() It is used to call a function contains this value and an argument list.
1. <script>
2. var add=new Function("num1","num2","return num1+num2");
3. [Link](add(2,5));
4. </script>
Output:
Example 2
Let's see an example to display the power of provided value.
1. <script>
2. var pow=new Function("num1","num2","return [Link](num1,num2)");
3. [Link](pow(2,3));
4. </script>
Output:
8
JavaScript Objects
A javaScript object is an entity having state and behavior (properties and method). For
example: car, pen, bike, chair, glass, keyboard, monitor etc.
JavaScript is template based not class based. Here, we don't create class to get the
object. But, we direct create objects.
1. By object literal
2. By creating instance of Object directly (using new keyword)
3. By using an object constructor (using new keyword)
1. object={property1:value1,property2:value2.....propertyN:valueN}
1. <script>
2. emp={id:102,name:"Shyam Kumar",salary:40000}
3. [Link]([Link]+" "+[Link]+" "+[Link]);
4. </script>
Output of the above example
102 Shyam Kumar 40000
1. <script>
2. var emp=new Object();
3. [Link]=101;
4. [Link]="Ravi Malik";
5. [Link]=50000;
6. [Link]([Link]+" "+[Link]+" "+[Link]);
7. </script>
Output of the above example
101 Ravi 50000
1. <script>
2. function emp(id,name,salary){
3. [Link]=id;
4. [Link]=name;
5. [Link]=salary;
6. }
7. e=new emp(103,"Vimal Jaiswal",30000);
8.
9. [Link]([Link]+" "+[Link]+" "+[Link]);
10. </script>
Output of the above example
103 Vimal Jaiswal 30000
Defining method in JavaScript Object
We can define method in JavaScript object. But before defining method, we need to
add property in the function with same name as method.
1. <script>
2. function emp(id,name,salary){
3. [Link]=id;
4. [Link]=name;
5. [Link]=salary;
6.
7. [Link]=changeSalary;
8. function changeSalary(otherSalary){
9. [Link]=otherSalary;
10. }
11. }
12. e=new emp(103,"Sonoo Jaiswal",30000);
13. [Link]([Link]+" "+[Link]+" "+[Link]);
14. [Link](45000);
15. [Link]("<br>"+[Link]+" "+[Link]+" "+[Link]);
16. </script>
Output of the above example
103 Sonoo Jaiswal 30000
103 Sonoo Jaiswal 45000
let a, b, c;
a = 2;
b = 3;
c = a + b;
[Link]("The value of c is " + c + ".");
Output
The value of c is 5.
Multiple statements on one line are allowed if they are separated with a
semicolon.
a=2;b=3;z=a+b;
2. Code Blocks: JavaScript statements can be grouped together inside curly
brackets. Such groups are known as code blocks. The purpose of grouping is to
define statements to be executed together.
Example: JavaScript function
• Javascript
function myFunction() {
[Link]("Hello");
[Link]("How are you?");
}
myFunction()
Output
Hello
How are you?
3. White Space: Javascript ignores multiple white spaces.
Example:
• Javascript
[Link](10 * 2);
[Link](10 * 2);
Output
20
20
Both the result will be the same
4. Line Length and Line Breaks: Javascript code’s preferred line length by most
programmers is up to 80 characters. The best place to break a code line in
Javascript, if it doesn’t fit, is after an operator.
Example:
[Link]("geek1").innerHTML =
"Hello Geek!";
5. Keywords: Keywords are reserved words and cannot be used as a variable
name. A Javascript keyword tells about what kind of operation it will perform.
Some commonly used keywords are:
• break and continue: break keyword is used to terminate a loop and
continue is used to skip a particular iteration in a loop and move to the
next iteration.
• do…. while: In this, the statements written within the do block are
executed till the condition in while is true.
• for: It helps in executing a block of statements till the condition is true.
• function: This keyword is used to declare a function.
• return: This keyword is used to exit a function.
• switch: This helps in executing a block of codes depending on different
cases.
• var, let, and const: These keywords are used to declare a variable in js.