Professional Documents
Culture Documents
Web Technology CS
Web Technology CS
Web Technology CS
UNIT-1
Structuring Documents for the Web: Introducing HTML and XHTML, Basic Text
Formatting, Presentational Elements, Phrase Elements, Lists, Editing Text, Core
Elements and Attributes, Attribute Groups. Links and Navigation: Basic Links,
Creating Links with the Element, Advanced E- mail Links. Images, Audio, and Video:
Adding Images Using the Error! Filename not specified. Element, Using Images as
Links Image Maps, Choosing the Right Image
UNIT-2
Tables: Introducing Tables, Grouping Section of a Table, Nested Tables, Accessing
Tables. Forms: Introducing Forms, Form Controls, Sending Form Data to the Server.
Frames: Introducing Frameset, Element, Creating Links between Frames, Setting a
Default Target Frame Using Element, Nested Framesets, Inline or Floating Frames
with <iframe>.
UNIT-3
Cascading Style Sheets: Introducing CSS, Where you can Add CSS Rules. CSS
Properties: Controlling Text, Text Formatting, Text Pseudo Classes, Selectors,
Lengths, Introducing the Box Model. More Cascading Style Sheets: Links, Lists,
Tables, Outlines, The :focus and :activate Pseudo classes Generated Content,
Miscellaneous Properties, Additional Rules, Positioning and Layout wit, Page Layout
CSS , Design Issues.
UNIT-4
Java Script: How to Add Script to Your Pages, Variables and Data Types – Statements
and Operators, Control Structures, Conditional Statements, Loop Statements –
Functions - Message box, Dialog Boxes, Alert Boxes, Confirm Boxes, Prompt Boxes
UNIT-5
Working with JavaScript: Practical Tips for Writing Scripts, JavaScript Objects:
Window Object - Document object - Browser Object - Form Object - Navigator object
Screen object - Events, Event Handlers, Forms – Validations, Form Enhancements,
JavaScript Libraries.
WEB TECHNOLOGY
SUBJECT CODE: 21UCS08 SEMESTER: V
UNIT - 1
WHAT IS HTML?
HTML stands for Hyper Text Markup Language
HTML is the standard markup language for creating Web pages
HTML describes the structure of a Web page
It provides some titles, headings, paragraphs, lists, tables, embedded images, etc
INTRODUCING HTML AND XHTML:
HTML consists of a series of elements
HTML elements tell the browser how to display the content
HTML elements label pieces of content such as "this is a heading", "this is a paragraph", "this is
a link", etc.
XHTML stands for EXtensible HyperText Markup Language
XHTML is a stricter, more XML-based version of HTML
XHTML is HTML defined as an XML application
XHTML is supported by all major browsers
Example
<!DOCTYPE html>
<html>
<head>
<title>formatting elements</title>
</head>
<body>
<h1>Explanation of formatting element</h1>
<p><strong>This is an important content</strong>, and this is normal content</p>
</body>
</html>
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.
Example:
<p> <i>Write Your First Paragraph in italic text.</i></p>
Output:
The HTML <em> tag is a logical element, which will display the enclosed content in italic
font, with added semantics importance.
example:
<p><em>This is an important content</em>, which displayed in italic font.</p>
Output:D
4) Underlined Text
If you write anything within <u>.........</u> element, is shown in underlined text.
Example:AD
<p> <u>Write Your First Paragraph in underlined text.</u></p>
Output:
5) Strike Text
Anything written within <strike>.......................</strike> element is displayed with
strikethrough. It is a thin line which cross the statement.
Example:
<p> <strike>Write Your First Paragraph with strikethrough</strike>.</p>
Output:
Write Your First Paragraph with strikethrough.
6) Monospaced Font
If you want that each letter has the same width then you should write the content within
<tt>.............</tt> element.
Example:
<p>Hello <tt>Write Your First Paragraph in monospaced font.</tt></p>
Output:
Hello Write Your First Paragraph in monospaced font.
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.
Example:
<p>Hello <sup>Write Your First Paragraph in superscript.</sup></p>
Output:
Hello Write Your First Paragraph in superscript.
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.
Example:
<p>Hello <sub>Write Your First Paragraph in subscript.</sub></p>
Output:
Hello Write Your First Paragraph in subscript.
9) Deleted Text
Anything that puts within <del>..........</del> is displayed as deleted text.
Example:
<p>Hello <del>Delete your first paragraph.</del></p>
Output:
Hello
Example:
<p>Hello <small>Write the paragraph in smaller font.</small></p>
Output:
Hello Write the paragraph in smaller font.
PRESENTATIONAL ELEMENTS:
There are a handful of (X)HTML elements that are explicitly presentation
oriented. Sometimes called “physical” styles, they provide instructions for
the size, weight, or style of the font used to display the element.
If you’ve been paying attention, you already know that Cascading Style
Sheets are now the preferred way to specify presentation instructions.
The presentational inline elements, along with the preferred alternative
for achieving the same visual effect.
PHRASE ELEMENTS:
The HTML phrase tags are special purpose tags, which defines the structural meaning of a
block of text or semantics of text. Following is the list of phrase tags, some of which we
have already discussed in HTML formatting.
o Abbreviation tag : <abbr>
o Acronym tag: <acronym>
o Marked tag: <mark>
o Strong tag: <strong>
o Emphasized tag : <em>
o Definition tag: <dfn>
o Quoting tag: <blockquote>
o Short quote tag : <q>
o Code tag: <code>
o Keyboard tag: <kbd>
o Address tag: <address>
3. Strong text:
This tag is used to display the important text of the content. The text written between
<strong> and </strong> will be displayed as important text.
Example
<p>In HTML it is recommended to use <strong>lower-case</strong>, while writing a code. </p>
OUTPUT:
4. Emphasized text
This tag is used to emphasize the text, and displayed the text in italic form. The text written
between <em> and </em> tag will italicized the text.
Example
<p>HTML is an <em>easy </em>to learn language.</p> Test it Now
Output:
5. Definition tag:
When you use the <dfn> and </dfn> tags, it allow to specify the keyword of the content.
Following is the example to show how to definition element.
Example
<p><dfn>HTML </dfn> is a markup language. </p> Test it Now
Output:
6. Quoting text:
The HTML <blockquote> element shows that the enclosed content is quoted from another
source.
The Source URL can be given using the cite attribute, and text representation of source can
display using <cite> ..... </cite>element.
Example
1. <blockquote cite="https://www.keepinspiring.me/famous-
quotes/"><p>?The first step toward success is taken when you refuse to be a captive of the environ
ment in which you first find yourself.?</p></blockquote>
2. <cite>-Mark Caine</cite> Test it Now
Output:
7. Short Quotations:
An HTML <q> ....... </q> element defines a short quotation.
If you will put any content between <q> ....... </q>, then it will enclose the text in double
quotes.
Example:
p>Steve Jobs said: <q> Have To Be Pushed. The Vision Pulls You.</q>?</p> Tesow
Output:
8. Code tags
The HTML <code> </code> element is used to display the part of computer code. It will
display the content in monospaced font.
EXAMPLE:
<p>First Java program</p>
<p><code>class Simple{ public static void main(String args[]){
System.out.println("Hello Java"); }} </code>
</p>
Output:
9. Keyboard Tag
In HTML the keyboard tag, <kbd>, indicates that a section of content is a user input from keyboard.
EXAMPLE
<p>Please press <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + t<kbd></kbd> to restore page on chrome.
</p>
Output:
10. Address tag
An HTML <address> tag defines the contact information about the author of the content.
The content written between <address> and </address> tag, then it will be displayed in
italic font.
<address> You can ask your queries by contact us on <a href="">example123@newdomain.com</a
>
<br> You can also visit at: <br>58 S. Garfield Street. Villa Rica, GA 30187.
</address> TNow
Output:
HTML Lists
HTML lists allow web developers to group a set of related items in lists.
Example An unordered HTML list:
Item
Item
Item
Item
1. First item
2. Second item
3. Third item
4. Fourth item
Example
<dl>
<dt>Coffee</dt>
<dd>- black hot drink</dd>
<dt>Milk</dt>
<dd>- white cold drink</dd>
</dl>
<br />
<!DOCTYPE html>
<html>
<head>
<title>Nested Elements Example</title>
</head>
<body>
<h1>This is <i>italic</i> heading</h1>
<p>This is <u>underlined</u> paragraph</p>
</body>
</html>
OUTPUT −
This is italic heading
There are two ways to specify the URL in the src attribute:
1.Absolute URL - Links to an external image that is hosted on another website.
Example: src="https://www.w3schools.com/images/img_girl.jpg".
2.Relative URL - Links to an image that is hosted within the website. Here, the URL does not include
the domain name. If the URL begins without a slash, it will be relative to the current page. Example:
src="img_girl.jpg".
<body>
<h1>GeeksforGeeks</h1>
<h2> HTML nav Tag</h2>
<!-- nav tag starts -->
<nav>
<a href="#">Home</a> |
<a href="#">Interview</a> |
<a href="#">Languages</a> |
<a href="#">Data Structure</a> |
<a href="#">Algorithm</a>
</nav>
<!-- nav tag ends -->
</body>
</html>
Output:
Try to click on the computer, phone, or the cup of coffee in the image below:
Example
<img src="workplace.jpg" alt="Workplace" usemap="#workmap">
<map name="workmap">
<area shape="rect" coords="34,44,270,350" alt="Computer" href="computer.htm">
<area shape="rect" coords="290,172,333,250" alt="Phone" href="phone.htm">
<area shape="circle" coords="337,300,44" alt="Coffee" href="coffee.htm">
</map>
Example:
<!DOCTYPE html>
<html>
<head>
<title>Adding Flash File</title>
</head>
<body>
<h2>Welcome To GFG</h2>
<object width="500" height="400">
<param name="movie" value="file.swf">
<embed src="file.swf" width="500" height="400">
</embed>
</object>
</body>
</html>
Output:
Audio and video to your web page in html:
HTML Audio
The HTML <audio> element is used to play an audio file on a web page.
The HTML <audio> Element
To play an audio file in HTML, use the <audio> element:
Example
<audio controls>
<source src="horse.ogg" type="audio/ogg">
<source src="horse.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<source> Defines multiple media resources for media elements, such as <video> and
<audio>
</video>
Attribute Description
controls It defines the video controls which is displayed with play/pause buttons.
poster It specifies the image which is displayed on the screen when the video is not
played.
autoplay It specifies that the video will start playing as soon as it is ready.
loop It specifies that the video file will start over again, every time when it is
completed.
preload It specifies the author view to upload video file when the page loads.
5. Which of the following is used to read an HTML page and render it?
a) Web server
b) Web network
c) Web browser
d) Web matrix
7. Which of the following tag is used for inserting the largest heading in HTML?
a) head
b) <h1>
c) <h6>
d) heading
13. Which of the following elements in HTML5 defines video or movie content?
a) <video>
b) <movie>
c) <audio>
d) <media>
14. Which of the following is not the element associated with the HTML table layout?
a) alignment
b) color
c) size
d) spanning
16. Which HTML tag is used for making character appearance bold?
a) <u>content</u>
b) <b>content</b>
c) <br>content</br>
d) <i>content</i>
17. Which HTML tag is used to insert an image?
a) <img url=”htmllogo.jpg” />
b) <img alt=”htmllogo.jpg” />
c) <img src=”htmllogo.jpg” />
d) <img link=”htmllogo.jpg” />
20. Which of the following HTML code will make an image clickable?
a)
<a href="https://www.sanfoundry.com/">Sanfoundry Home Page</a>
b)
<img src="https://www.sanfoundry.com/sanfoundry-logo">
<a href="https://www.sanfoundry.com/">Sanfoundry Home Page</a>
</img>
c)
<a href="https://www.sanfoundry.com/">Sanfoundry Home Page</a>
<img src="https://www.sanfoundry.com/sanfoundry-logo" />
d)
<a href="https://www.sanfoundry.com/"><img
src="https://www.sanfoundry.com/sanfoundry-logo" /></a>
22. In HTML, which attribute is used to create a link that opens in a new window tab?
a) src=”_blank”
b) alt=”_blank”
c) target=”_self”
d) target=”_blank”
26. Which of the following HTML tag is used to add a row in a table?
a) <th>
b) <td>
c) <tr>
d) <tt>
28. Which of the following tag is used to create a text area in HTML Form?
a) <textarea> </textarea>
b) <text></text>
c) <input type=”text” />
d) <input type=”textarea” />
37. Which HTML tag is used to convert the plain text into italic format?
a) <b>
b) <p>
c) <i>
d) <a>
43. Which of the following is an HTML specification used to add more information to
HTML tags?
a) Modifydata
b) Minidata
c) Macrodata
d) Microdata
45. Which of the following HTML element is used for canvas graphics?
a) <css>
b) <paint>
c) <canvas>
d) <graphic>
5 – MARKS:
1. How to creating links with the <a> elements?
2. Explain about HTML tags?
3. Discuss about Attributes?
4. Explain adding flash?
5. Discuss about advanced E-mail links?
10 – MARKS:
1. Explain about video and audio to your web pages?
2. Discuss about link?
3. Explain detail about HTML?
4. Explain Basic Text Formatting?
UNIT- 2
TABLES:
The HTML tables allow web authors to arrange data like text, images, links, other
tables, etc. into rows and columns of cells.
The HTML tables are created using the <table> tag in which the <tr> tag is used to
create table rows and <td> tag is used to create data cells. The elements under
<td> are regular and left aligned by default.
Example Live Demo
<!DOCTYPE html>
<html>
<head>
<title>HTML Tables</title>
</head>
<body>
<table border = "1">
<tr>
<td>Row 1, Column 1</td>
<td>Row 1, Column 2</td>
</tr>
<tr>
<td>Row 2, Column 1</td>
<td>Row 2, Column 2</td>
</tr>
</table>
</body>
</html>
Table Heading
Table heading can be defined using <th> tag. This tag will be put to replace <td>
tag, which is used to represent actual data cell.
Normally you will put your top row as table heading as shown below, otherwise
you can use <th> element in any row. Headings, which are defined in <th> tag are
centered and bold by default.
Example Live Demo
<!DOCTYPE html>
<html>
<head>
<title>HTML Table Header</title>
</head>
<body>
<table border = "1">
<tr>
<th>Name</th>
<th>Salary</th>
</tr>
<tr>
<td>Ramesh Raman</td>
<td>5000</td>
</tr>
<tr>
<td>Shabbir Hussein</td>
<td>7000</td>
</tr>
</table>
</body>
</html>
This will produce the following result –
Name Salary
Ramesh Raman 5000
Shabbir Hussein 7000
<head>
<title>HTML Table Cellpadding</title>
</head>
<body>
<table border = "1" cellpadding = "5" cellspacing = "5">
<tr>
<th>Name</th>
<th>Salary</th>
</tr>
<tr>
<td>Ramesh Raman</td>
<td>5000</td>
</tr>
<tr>
<td>Shabbir Hussein</td>
<td>7000</td>
</tr>
</table>
</body>
</html>
Name Salary
Tables Backgrounds
You can set table background using one of the following two ways −
bgcolor attribute − You can set background color for whole table or just for one
cell.
background attribute − You can set background image for whole table or just for
one cell.
You can also set border color also using bordercolor attribute.
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Table Background</title>
</head>
<body>
<table border = "1" bordercolor = "green" bgcolor = "yellow">
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
<tr>
<td rowspan = "2">Row 1 Cell 1</td>
<td>Row 1 Cell 2</td>
<td>Row 1 Cell 3</td>
</tr>
<tr>
<td>Row 2 Cell 2</td>
<td>Row 2 Cell 3</td>
</tr>
<tr>
<td colspan = "3">Row 3 Cell 1</td>
</tr>
</table>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>HTML Table Background</title>
</head>
<body>
<table border = "1" bordercolor = "green" background = "/images/test.png">
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
<tr>
<td rowspan = "2">Row 1 Cell 1</td>
<td>Row 1 Cell 2</td><td>Row 1 Cell 3</td>
</tr>
<tr>
<td>Row 2 Cell 2</td>
<td>Row 2 Cell 3</td>
</tr>
<tr>
<td colspan = "3">Row 3 Cell 1</td>
</tr>
</table>
</body>
</html>
Output:
Column 1 Column 2 Column 3
Row 1 Cell 2 Row 1 Cell 3
Row 1 Cell 1
Row 2 Cell 2 Row 2 Cell 3
Row 3 Cell 1
Nested Tables
You can use one table inside another table. Not only tables you can use almost all the
tags inside table data tag <td>.
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Table</title>
</head>
<body>
<table border = "1" width = "100%">
<tr>
<td>
<table border = "1" width = "100%">
<tr>
<th>Name</th>
<th>Salary</th>
</tr>
<tr>
<td>Ramesh Raman</td>
<td>5000</td>
</tr>
<tr>
<td>Shabbir Hussein</td>
<td>7000</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
Output:
Name Salary
Ramesh Raman 5000
Shabbir Hussein 7000
HTML Forms
An HTML form is used to collect user input. The user input is most often sent to a
server for processing.
HTML Forms are required, when you want to collect some data from the site
visitor.
For example, during user registration you would like to collect information such
as name, email address, credit card, etc.
HTML Form Controls:
There are different types of form controls that you can use to collect data using HTML
form −
Text Input Controls
Checkboxes Controls
Radio Box Controls
Select Box Controls
File Select boxes
Hidden Controls
Clickable Buttons
Submit and Reset Button
The <form> Element
The HTML <form> element is used to create an HTML form for user input:
<form>
.
form elements
.
</form>
The <form> element is a container for different types of input elements:
text fields, checkboxes, radio buttons, submit buttons, etc.
The <input> Element
The HTML <input> element is the most used form element.
An <input> element can be displayed in many ways, depending on the type attribute.
Type Description
Text Fields
The <input type="text"> defines a single-line input field for text input.
Example:
<form>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname">
</form>
OUTPUT:
First name:
Last name:
CSS
JavaScript
Checkboxes
The <input type="checkbox"> defines a checkbox.
Checkboxes let a user select ZERO or MORE options of a limited number of choices.
Example:
<form>
<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">
<label for="vehicle1"> I have a bike</label><br>
<input type="checkbox" id="vehicle2" name="vehicle2" value="Car">
<label for="vehicle2"> I have a car</label><br>
<input type="checkbox" id="vehicle3" name="vehicle3" value="Boat">
<label for="vehicle3"> I have a boat</label>
</form>
OUTPUT:
I have a bike
I have a car
I have a boat
The Submit Button
The <input type="submit"> defines a button for submitting the form data to a form-handler.
Example:
<form action="/action_page.php">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
</form>
OUPUT:
First name:
John
Last name:
Doe
Submit
key1=value1&key2=value2
FRAMES:
HTML frames are used to divide your browser window into multiple sections
where each section can load a separate HTML document.
Introducing Frameset:
A collection of frames in the browser window is known as a frameset. The
window is divided into frames in a similar way the tables are organized: into rows
and columns.
SYNTAX:
<frameset cols=" ">............</frameset>
Example:
!DOCTYPE html>
<html>
<head>
<title>Frame tag</title>
</head>
<frameset cols="50%,50%">
<frame src="https://www.javatpoint.com/html-table">
<frame src="https://www.javatpoint.com/css-table">
</frameset>
</html>
Output:
cols Pixels It specifies the number and size of column spaces in the
% frameset. (Not Supported in HTML5)
*
rows Pixels It specifies the number and size of the rows spaces in the
% frameset. (Not Supported in HTML5)
*
src
1 This attribute is used to give the file name that should be loaded in the frame.
Its value can be any URL. For example, src = "/html/top_frame.htm" will load
an HTML file available in html directory.
name
This attribute allows you to give a name to a frame. It is used to indicate which
2 frame a document should be loaded into. This is especially important when
you want to create links in one frame that load pages into an another frame,
in which case the second frame needs a name to identify itself as the target of
the link.
frameborder
3 This attribute specifies whether or not the borders of that frame are shown; it
overrides the value given in the frameborder attribute on the <frameset> tag
if one is given, and this can take values either 1 (yes) or 0 (no).
marginwidth
4 This attribute allows you to specify the width of the space between the left
and right of the frame's borders and the frame's content. The value is given in
pixels. For example marginwidth = "10".
marginheight
5
This attribute allows you to specify the height of the space between the top
and bottom of the frame's borders and its contents. The value is given in
pixels. For example marginheight = "10".
noresize
6 By default, you can resize any frame by clicking and dragging on the borders of
a frame. The noresize attribute prevents a user from being able to resize the
frame. For example noresize = "noresize".
scrolling
7 This attribute controls the appearance of the scrollbars that appear on the
frame. This takes values either "yes", "no" or "auto". For example scrolling =
"no" means it should not have scroll bars.
longdesc
8 This attribute allows you to provide a link to another page containing a long
description of the contents of the frame. For example longdesc =
"framedescription.htm"
<head>
</head>
<frameset cols="25%,75%">
</frameset>
</html>
Attribute Values:
_blank: It opens the link in a new window.
_self: It opens the linked document in the same frame. it is the default value.
_parent: It opens the linked document in the parent frameset.
_top: It opens the linked document in the full body of the window.
framename: It opens the linked document in the named frame.
Example: This example illustrates the use of target attribute in the <base>
element.
Example:
<!DOCTYPE html>
<html>
<head>
<base target="_self">
<title>
HTML Base target Attribute
</title>
</head>
<body style="text-align:center;">
<h1 style="color:green;">
GeeksForGeeks
</h1>
</html>
Output:
4. Which of the following is not the element associated with HTML table layout?
a) size
b) spanning
c) alignment
d) color
12. Which of the following is not the value for frame attribute?
a) above
b) void
c) none
d) box
13. Which of the following is not the value for rules attribute?
a) vsides
b) rows
c) all
d) groups
14. Which of the following is not the value for align attribute?
a) justify
b) char
c) middle
d) left
31. Which inline function embeds an independent HTML document into current document?
A. <div>
B. <span>
C. <form>
D. <iframe>
38. Which attribute of the frameset tag creates two horizontal frames?
A. Cols
B. Rows
C. Both
D. None
39. Which inline function embeds an independent HTML document into current document?
A. <div>
B. <span>
C. <form>
D. <iframe>
5 – Mark:
1. Explain Nested tables?
2. Discuss sending a default target frame using <base> elements?
3. Explain Nested frameset?
4. Sending form data to the server?
10 – Mark:
1. Explain details about Tables with example?
2. Discuss about Forms with example?
3. Explain about Frames with example?
4. Explain Inline or Floating frames with <iframe>?
UNIT - 3
Cascading Style Sheets (CSS):
Introducing css:
Cascading Style Sheets (CSS) is used to format the layout of a webpage.
With CSS, you can control the color, font, the size of text, the spacing between
elements, how elements are positioned and laid out, what background images or
background colors
CSS properties:
Properties-name Syntax Description
OUTPUT:
This is a heading
This is a paragraph.
CSS Border
The CSS border property defines a border around an HTML element.
Example
p{
border: 2px solid powderblue;
}
OUPUT:
This is a heading
This is a paragraph.
This is a paragraph.
This is a paragraph.
CSS Padding
The CSS padding property defines a padding (space) between the text and the
border.
Example
p{
border: 2px solid powderblue;
padding: 30px;
}
OUTPUT:
This is a heading
This is a paragraph.
This is a paragraph.
This is a paragraph.
CSS Margin
The CSS margin property defines a margin (space) outside the border.
Example:
p{
border: 2px solid powderblue;
margin: 50px;
}
CONTROLLING TAXT:
The CSS text formatting properties are used to format text, style the text and
perform different types of manipulations like word spacing, alignment,
justification, and text transformation.
Syntax:
The Syntax to write this property:
Selector {
property-name : /*value*/
}
TEXT FORMATTING:
<body>
<p style = "color:red;">
This text will be written in red.
</p>
</body>
</html>
OUTPUT:
This text will be written in red.
EXAMPLE:
<html>
<head>
</head>
<body>
<p style = "direction:rtl;">
This text will be rendered from right to left
</p>
</body>
</html>
OUTPUT:
This text will be renedered from right to left
<html>
<head>
</head>
<body>
<p style = "letter-spacing:5px;">
This text is having space between letters.
</p>
</body>
</html>
OUTPUT:
T h i s t e x t i s h a v i n g s p a c e b e t w e e n l e t t e r s .
<html>
<head>
</head>
<body>
<p style = "word-spacing:5px;">
This text is having space between words.
</p>
</body>
</html>
OUTPUT:
This text is having space between words.
<html>
<head>
</head>
<body>
<p style = "text-indent:1cm;">
This text will have first line indented by 1cm and this line will remain at
its actual position this is done by CSS text-indent property.
</p>
</body>
</html>
OUTPUT:
This text will have first line indented by 1cm and this line will remain at its actual
position this is done by CSS text-indent property
<html>
<head>
</head>
<body>
<p style = "text-align:right;">
This will be right aligned.
</p>
<html>
<head>
</head>
<body>
<p style = "text-transform:capitalize;">
This will be capitalized
</p>
<body>
<p style = "text-shadow:4px 4px 8px blue;">
If your browser supports the CSS text-shadow property,
this text will have a blue shadow.
</p>
</body>
</html>
OUTPUT:
If your browser supports the CSS text-shadow property, this text will have a blue
shadow.
The pseudo-classes
A pseudo-class can be defined as a keyword which is combined to a selector that defines
the special state of the selected elements.
It is added to the selector for adding an effect to the existing elements based on their
states. For example, The ":hover"
A pseudo-class starts with a colon (:)
Syntax
selector: pseudo-class
{
property: value;
}
pseudo- Description
class
:hover IIt adds special effects to an element when the user moves the mouse
pointer over the element.
:focus IIt selects the element which is focused by the user currently.
:first-child It adds special effects to an element, which is the first child of another
element.
:hover pseudo-class
<html>
<head>
<style>
body{
text-align:center;
}
h1:hover{
color:red;
}
h2:hover{
color:blue;
}
</style>
</head>
<body>
<h1>Hello world </h1>
<h2>This is an example of :hover pseudo class</h2>
</body>
</html>
OUTPUT:
Hello world
Me too!
And me!
OUTPUT:
Demonstrating the Box Model
The CSS box model is essentially a box that wraps around every HTML element. It
consists of: borders, padding, margins, and the actual content.
This text is the content of the box. We have added a 50px padding, 20px margin and a
15px green border. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
o list-style-type: This property is responsible for controlling the appearance and shape of the
marker.
o list-style-image: It sets an image for the marker instead of the number or a bullet point.
o list-style-position: It specifies the position of the marker.
o list-style: It is the shorthand property of the above properties.
o marker-offset: It is used to specify the distance between the text and the marker. It is
unsupported in IE6 or Netscape 7.
Example:
<!DOCTYPE html>
<html>
<head>
<title>CSS Lists</title>
<style>
.num{
list-style-type:decimal;
}
.alpha{
list-style-type:lower-alpha;
}
.roman{
list-style-type:lower-roman;
}
.circle{
list-style-type:circle;
}
.square{
list-style-type:square;
}
.disc{
list-style-type:disc;
}
</style>
</head>
<body>
<h1>
Welcome to the javaTpoint.com
</h1>
<h2>
Ordered Lists
</h2>
<ol class="num">
<li>one</li>
<li>two</li>
<li>three</li>
</ol>
<ol class="alpha">
<li>one</li>
<li>two</li>
<li>three</li>
</ol>
<ol class="roman">
<li>one</li>
<li>two</li>
<li>three</li>
</ol>
<h2>
Unordered lists
</h2>
<ul class="disc">
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
<ul class="circle">
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
<ul class="square">
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
</body>
</html>
OUTPUT:
Welcome to the javaTpoint.com
Ordered Lists
1. one
2. two
3. three
a. one
b. two
c. three
i. one
ii. two
iii. three
Unordered lists
one
two
three
o one
o two
o three
one
two
CSS Table
apply style on HTML tables for better look and feel. There are some CSS properties that are widely
used in designing table using CSS:
o border
o border-collapse
o padding
o width
o height
o text-align
o color
o background-color
CSS Table Border
<style>
table, th, td {
border: 1px solid black;
}
</style>
CSS Table Border Collapse
<style>
table, th, td {
border: 2px solid black;
border-collapse: collapse;
}
</style>
CSS Table Padding
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 10px;
}
</style>
OUTPUT:
CSS Outline
CSS outline is just like CSS border property. It facilitates you to draw an extra border around
an element to get visual attention.
label{
width: 30%;
}
input{
background-color: default;
float: right;
}
input:focus{
background-color: grey;
}
h1, h2{
color: green;
text-align: center;
}
</style>
</head>
<body>
<h1>Geeks For Geeks</h1>
<h2>:focus Pseudo-class</h2>
<form>
<label for="username">Username:</label>
<input type="text" name="username"
placeholder="Enter your username" />
<br>
<label for="emailid">Email-Id:</label>
<input type="email" name="emailid"
placeholder="Enter your email-id" />
<label for="Password">Password:</label>
<input type="password" name="Password"
placeholder="Enter your password" />
</form>
</body>
</html>
Output:
:active Pseudo-class:
This pseudo-class is used to select an element that is activated when the user clicks on
it. The following example demonstrates that when you click on the box, its background
color changes for a moment.
Example: This example shows the active pseudo-class in CSS.
<!DOCTYPE html>
<html>
<head>
<title>CSS transition-property property</title>
<style>
.box{
background-color: yellow;
width: 300px;
height: 200px;
margin: auto;
font-size: 40px;
text-align: center;
}
.box:active{
background-color: orange;
}
h1, h2{
color: green;
text-align: center;
}
</style>
</head>
<body>
<h1>Geeks For Geeks</h1>
<h2>:active Pseudo-class</h2>
<div class="box">
My color changes for a moment if you click me!
</div>
</body>
</html>
Output:
Miscellaneous properties:
CSS Class Name HTML elements on which it is Properties defined
applied
he @import: rule imports another style sheet into the current style sheet.
The @charset rule indicates the character set the style sheet uses.
The @font-face rule is used to exhaustively describe a font face for use in a document.
The !important rule indicates that a user-defined rule should take precedence over the
author's style sheets.
The @import rule
The @import rule allows you to import styles from another style sheet. It should appear right at the
start of the style sheet before any of the rules, and its value is a URL.
So, if you wanted to make sure that a property always applied, you would add the
<html>
<head>
<style type = "text/css">
p { color: #ff0000 !important; }
p { color: #000000; }
</style>
</head>
<body>
<p>Tutorialspoint.com</p>
</body>
</html>
OUTPUT:
Tutorialspoint.com
Positioning and lagout wit:
The position Property
The position property specifies the type of positioning method used for an element.
There are five different position values:
static
relative
fixed
absolute
sticky
Elements are then positioned using the top, bottom, left, and right properties. However, these
properties will not work unless the position property is set first. They also work differently
depending on the position value.
position: static;
HTML elements are positioned static by default.
Static positioned elements are not affected by the top, bottom, left, and right properties.
An element with position: static; is not positioned in any special way; it is always positioned
according to the normal flow of the page:
This <div> element has position: static;
Example:
div.static {
position: static;
border: 3px solid #73AD21;
}
position: relative;
An element with position: relative; is positioned relative to its normal position.
Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it
to be adjusted away from its normal position. Other content will not be adjusted to fit into any
gap left by the element.
This <div> element has position: relative;
Example:
div.relative {
position: relative;
left: 30px;
border: 3px solid #73AD21;
}
position: fixed;
An element with position: fixed; is positioned relative to the viewport, which means it always
stays in the same place even if the page is scrolled. The top, right, bottom, and left properties
are used to position the element.
A fixed element does not leave a gap in the page where it would normally have been located.
div.fixed {
position: fixed;
bottom: 0;
right: 0;
width: 300px;
border: 3px solid #73AD21;
}
position: absolute;
An element with position: absolute; is positioned relative to the nearest positioned ancestor
(instead of positioned relative to the viewport, like fixed).
However; if an absolute positioned element has no positioned ancestors, it uses the document
body, and moves along with page scrolling.
div.relative {
position: relative;
width: 400px;
height: 200px;
border: 3px solid #73AD21;
}
div.absolute {
position: absolute;
top: 80px;
right: 0;
width: 200px;
height: 100px;
border: 3px solid #73AD21;
}
position: sticky;
An element with position: sticky; is positioned based on the user's scroll position.
A sticky element toggles between relative and fixed, depending on the scroll position.
div.sticky {
position: -webkit-sticky; /* Safari */
position: sticky;
top: 0;
background-color: green;
border: 2px solid #4CAF50;
}
A CSS layout can have header, footer, left pane, right pane and body part. Let's see a simple
example of CSS layout.
CSS layout example
<!DOCTYPE html>
<html>
<head>
<style>
.header{margin:-8px -8px 0px;background-image:linear-gradient(145deg,#7379ff,#b524ef);color:white;text-
align:center;padding:10px;}
.container{width:100%}
.left{width:15%;float:left;}
.body{width:65%;float:left;background-color:pink;padding:5px;}
.right{width:15%;float:left;}
.footer{margin:-8px;clear:both;background-image:linear-
gradient(145deg,#7379ff,#b524ef);color:white;text-align:center;padding:10px;}
</style>
</head>
<body>
<div class="header"><h2>JavaTpoint</h2></div>
<div class="container">
<div class="left">
<p>Left Page</p>
</div>
<div class="body">
<h1>Body Page</h1>
<p>Page Content goes here</p><p>Page Content goes here</p><p>Page Content goes here</p>
<p>Page Content goes here</p><p>Page Content goes here</p><p>Page Content goes here</p>
<p>Page Content goes here</p><p>Page Content goes here</p><p>Page Content goes here</p>
<p>Page Content goes here</p><p>Page Content goes here</p><p>Page Content goes here</p>
<p>Page Content goes here</p>
</div>
<div class="right">
<p>Right Page</p>
</div>
</div>
<div class="footer">
<p>Footer</p>
</div>
</body>
</html>
Output:
Design Issues:
It was based on a table architecture system.
Styling was not free of content.
Unstructured design work.
Fewer media applications.
No proper functionality for high graphic elements.
Fewer animations.
Less dynamic design (mainly static)
Low presentation reflexes.
ONE MARK:
1. What is CSS?
a) CSS is a style sheet language
b) CSS is designed to separate the presentation and content, including layout, colors, and fonts
c) CSS is the language used to style the HTML documents
d) All of the mentioned
2. Which of the following tag is used to embed css in html page?
a) <css>
b) <!DOCTYPE html>
c) <script>
d) <style>
3. Which of the following CSS selectors are used to specify a group of elements?
a) tag
b) id
c) class
d) both class and tag
4. Which of the following has introduced text, list, box, margin, border, color, and background
properties?
a) HTML
b) PHP
c) CSS
d) Ajax
5. Which of the following CSS framework is used to create a responsive design?
a) django
b) rails
c) larawell
d) bootstrap
6. Which of the following CSS selector is used to specify a rule to bind a particular unique element?
a) tag
b) id
c) class
d) both class and tag
7. Which of the following type of HTML tag is used to define an internal style sheet?
a) <script>
b) <link>
c) <class>
d) <style>
8. Which of the following CSS property is used to make the text bold?
a) text-decoration: bold
b) font-weight: bold
c) font-style: bold
d) text-align: bold
9. What will be the output of following CSS code snippet?
h1 {color: "green";}
a) nothings happen
b) error occurs
c) heading becomes dark-green
d) heading becomes green
10. Which of the following CSS style property is used to specify an italic text?
a) style
b) font
c) font-style
d) @font-face
11. What will be the output of following CSS code snippet?
h1 {color: red text-decoration: underline; font-style: italic;}
a) color: red, text-decoration: underline works
b) only font-style: italic works
c) color: red, text-decoration: underline and font-style: italic all works
d) text-decoration: underline and font-style: italic works
12. Which of the following are the CSS Extension Prefixes for Webkit?
a) -chrome
b) -web
c) -o-
d) -webkit.
13. Which of the following is the correct syntax to link an external style sheet in the HTML file?
a) <link rel=”stylesheet” href=”style.css” />
b) <link rel=”stylesheet” src=”style.css” />
c) <style rel=”stylesheet” src=”style.css” />
d) <style rel=”stylesheet” link=”style.css” />
View Answer
14. Which of the following is the first CSS specification to become an official W3C
Recommendation?
a) CSS level 2
b) (X)HTML CSS
c) CSS level 1
d) CSS level 2.1
15. Which of the following function defines a linear gradient as a CSS image?
a) gradient()
b) linear-gradient()
c) grayscale()
d) image()
16. Which of the following CSS property can be used to set the image as a border instead of the
border style?
a) background-image-source
b) background-image
c) border-image-source
d) border-image
17. Which of the following CSS property defines the different properties of all four sides of an
element’s border in a single declaration?
a) border-collapse
b) border-width
c) padding
d) border
18. Which of the following is the correct way to apply CSS Styles?
a) in an external CSS file
b) inside an HTML element
c) inside the <head> section of an HTML page
d) all of the mentioned
19. Which of the following CSS property sets the font size of text?
a) font-size
b) text-size
c) text
d) size
20. Which of the following is not the property of the CSS box model?
a) margin
b) color
c) width
d) height
21. Which of the following CSS property sets what kind of line decorations are added to an
element, such as underlines, overlines, etc?
a) text-decoration
b) text-style
c) text-decoration-line
d) text-line
22. Which of the following CSS property specifies the look and design of an outline?
a) outline-style
b) outline-format
c) outline-font
d) none of the mentioned
23. What will be the output of the following CSS code snippet?
span {
border: 1px solid red;
outline: green dotted thick;
}
a) All span elements will have a green thick border and a red outline
b) All span elements will have a red border and a green dotted outline
c) All span elements will have a outer green dotted border and an inner red border
d) All span elements will have an outer red border and inner green dotted border
24. Which of the following CSS property sets the shadow for a box element?
a) set-shadow
b) box-shadow
c) shadow
d) canvas-shadow
25. Which of the following CSS property is used to set the color of the text?
a) text-decoration
b) pallet
c) colour
d) color
26. Which of the following CSS Property controls how an element is positioned?
a) static
b) position
c) fix
d) set
27. Which of the following CSS selector selects the elements that are checked?
a) :checked
b) E ~ F
c) ::after
d) none of the mentioned
28. Which of the following is an appropriate value for the overflow element?
a) scroll
b) hidden
c) auto
d) all of the mentioned
29. Which of the following CSS property is used to specify table borders in CSS?
a) table:border
b) table
c) border
d) none of the mentioned
30. Which of the following property is used to align the text in a table?
a) text-align
b) align
c) text
d) none of the mentioned
31. Which of the following CSS Property sets the stacking order of positioned elements?
a) y-index
b) z-index
c) x-index
d) all of the mentioned
View Answer
32. What will be the output of the following CSS code?
div {
border-width:5px;
border-style:dotted solid double dashed;
}
a) Box having dotted bottom outline, solid right outline, double top outline and dashed left outline
b) Box having dotted bottom outline, solid left outline, double top outline and dashed left outline
c) Box having dotted top outline, solid right outline, double bottom outline and dashed left
outline
d) Box having dotted top outline, solid left outline, double bottom outline and dashed right outline
33. Which of the following property allows a marquee to be used in the case of a text-overflow?
a) overflow-marquee
b) overflow-style
c) overflow-text
d) none of the mentioned
34. Which of the following CSS property defines the space between cells in a table?
a) border-spacing
b) border-style
c) border
d) none of the mentioned
35. Which of the following CSS3 property can be used to allow line breaks within words?
a) line-break
b) line-wrap
c) word-wrap
d) word-break
36. Which of the following is not a combinatory?
a) *
b) >
c) ~
d) +
37. Which of the following selects all elements that are siblings of the specified element?
a) child selector
b) general sibling selector
c) adjacent sibling selector
d) descendant selector
38. Which of the following adds the value of a counter to an element?
a) counter-increment
b) content
c) counter()
d) counter-reset
39. Which of the following specifies how to slice border image?
a) border-image
b) border-image-source
c) border-image-outset
d) border-image-slice
40. Which web font was developed by Apple and Microsoft in the late 1980s?
a) TTF
b) OTF
c) WOFF
d) WOFF 2.0
41. Which of the following is a format for scalable computer fonts?
a) EOT
b) WOFF
c) OTF
d) WOFF 2.0
42. Which font format is used in web pages?
a) WOFF 2.0
b) WOFF
c) SVG Fonts
d) EOT
43. What value lets animation to play forwards first then backward?
a) alternate
b) alternate-reverse
c) reverse
d) normal
44. Which value specifies animation with slow start?
a) ease-out
b) ease-in
c) linear
d) ease
45. How many values are there for animation-full-mode property?
a) 1
b) 2
c) 3
d) 4
46. Which of the following specifies animation code?
a) @keyframes
b) animation
c) animation-delay
d) animation-direction
47. Which specifies the speed curve of animation?
a) animation-iteration-count
b) animation-name
c) animation-play-state
d) animation-timing-function
48. How many values are there for the object-fit property?
a) 2
b) 5
c) 3
d) 4
49. Which property specifies the number of columns an element should be divided into?
a) column-rule
b) column-count
c) column-gap
d) column-fill
50. Which property is used for setting column-width and column-count?
a) columns
b) column-width
c) column-span
d) column-width-rule
5 – MARK
1.Explain CSS Properties?
2.Discuss text formatting?
3.Explain text pseudo?
4.Explain page layout?
10 – Mark:
1. Explain details about css with example?
2. Discuss about links, list, tables with example?
3. Explain about miscellaneous properties with example?
4. Explain the :focus and :active pseudo classes generated content?
UNIT-4
JavaScript
JavaScript (JS) is a lightweight, interpreted, or just-in-time
compiled programming language with first-class functions.
While it is most well-known as the scripting language for Web pages, many non-browser
environments also use it,
JavaScript is a prototype-based,
multi-paradigm, single-threaded, dynamic language, supporting object-oriented, imperative, and
declarative (e.g. functional programming) styles.
HISTORY:
JavaScript was invented by Brendan Eich in 1995.
It was developed for Netscape 2, and became the ECMA-262 standard in 1997.
After Netscape handed JavaScript over to ECMA, the Mozilla foundation continued to develop
JavaScript for the Firefox browser. Mozilla's latest version was 1.8.5. (Identical to ES5).
Internet Explorer (IE4) was the first browser to support ECMA-262 Edition 1 (ES1).
<script type="text/javascript">
alert("This alert box was called with the onload event");
</script>
When using the script tag, we must always use the attribute name and value
of type="text/javascript".
Using the script tag to include an external JavaScript file
To include an external JavaScript file, we can use the script tag with the
attribute src.
You've already used the src attribute when using images. The value for the src
attribute should be the path to your JavaScript file.
This script tag should be included between the <head> tags in your HTML
document.
JavaScript Files
JavaScript files are not HTML files or CSS files.
Always end with the js extension
Only include JavaScript
It's customary to put all JavaScript files in a folder called js on websites, like so:
JavaScript Variable
A JavaScript variable is simply a name of storage location. There are two types of variables in
JavaScript : local variable and global variable.
1. JavaScript variable
2. JavaScript Local variable
3. JavaScript Global variable
There are some rules while declaring a JavaScript variable (also known as identifiers).
1. Name must start with a letter (a to z or A to Z), underscore( _ ), or dollar( $ ) sign.
2. After first letter we can use digits (0 to 9), for example value1.
3. JavaScript variables are case sensitive, for example x and X are different variables.
var x = 10;
var _value="sonoo";
<script>
var x = 10;
var y = 20;
var z=x+y;
document.write(z);
</script>
Output of the above example
30
A JavaScript local variable is declared inside block or function. It is accessible within the function or
block only. For example:
<script>
function abc(){
var x=10;//local variable
}
</script>
Or,
<script>
If(10<13){
var y=20;//JavaScript local variable
}
</script>
A JavaScript global variable is accessible from any function. A variable i.e. declared outside the
function or declared with window object is known as global variable. For example:
<script>
var data=200;//gloabal variable
function a(){
document.writeln(data);
}
function b(){
document.writeln(data);
}
a();//calling JavaScript function
b();
</script>
There are five types of primitive data types in JavaScript. They are as follows:
JavaScript Operators
JavaScript operators are symbols that are used to perform operations on operands. For example:
var sum=10+20;
Here, + is the arithmetic operator and = is the assignment operator.
There are following types of operators in JavaScript.
1. Arithmetic Operators
2. Comparison (Relational) Operators
3. Bitwise Operators
4. Logical Operators
5. Assignment Operators
6. Special Operators
Arithmetic operators are used to perform arithmetic operations on the operands. The following
operators are known as JavaScript arithmetic operators.
+ Addition 10+20 = 30
- Subtraction 20-10 = 10
/ Division 20/10 = 2
The JavaScript comparison operator compares the two operands. The comparison operators are as
follows:
The bitwise operators perform bitwise operations on operands. The bitwise operators are as
follows:
= Assign 10+10 = 20
Operator Description
(?:) Conditional Operator returns value based on the condition. It is like if-else.
JavaScript If-else
The JavaScript if-else statement is used to execute the code whether condition is true or false.
There are three forms of if statement in JavaScript.
1. If Statement
2. If else statement
3. if else if statement
JavaScript If statement
It evaluates the content only if expression is true.
Syntax:
if(expression){
//content to be evaluated
}
Flowchart of JavaScript If statement
javaScript Switch
The JavaScript switch statement is used to execute one code from multiple expressions.
It is just like else if statement that we have learned in previous page. But it is convenient
than if..else..if because it can be used with numbers, characters etc.
switch statement syntax:
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;
}
JavaScript Loops
The JavaScript loops are used to iterate the piece of code using for, while, do while or for-in loops.
It makes the code compact. It is mostly used in array.
There are four types of loops in JavaScript.
1. for loop
2. while loop
3. do-while loop
4. for-in 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);
Let’s see the simple example of do while loop in javascript.
<script>
var i=21;
do{
document.write(i + "<br/>");
i++;
}while (i<=25);
</script>
Output:
21
22
23
24
25
The JavaScript for in loop is used to iterate the properties of an object. We will discuss about it
later.
JavaScript Functions
JavaScript functions are used to perform operations. We can call JavaScript function many times to
reuse the code.
Advantage of JavaScript function
There are mainly two advantages of JavaScript functions.
1. Code reusability: We can call a function several times so it save coding.
2. Less coding: It makes our program compact. We don’t need to write many lines of code each time to
perform a common task.
Let’s see the simple example of function in JavaScript that does not has arguments.
<script>
function msg(){
alert("hello! this is message");
}
</script>
<input type="button" onclick="msg()" value="call function"/>
Output of the above example
CALL FUNCTION
We can call function by passing arguments. Let’s see the example of function that has one
argument.
<script>
function getcube(number){
alert(number*number*number);
}
</script>
<form>
<input type="button" value="click" onclick="getcube(4)"/>
</form>
Output of the above example
AD
CLICK
We can call function that returns a value and use it in our program. Let’s see the example of
function that returns value.
<script>
function getInfo(){
return "hello javatpoint! How r u?";
}
</script>
<script>
document.write(getInfo());
</script>
Syntax
Parameter
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.
Example 1
<script>
var add=new Function("num1","num2","return num1+num2");
document.writeln(add(2,5));
</script>
Output:
7
JavaScript provides built-in global functions to display popup message boxes for different
purposes.
alert(message): Display a popup box with the specified message with the OK button.
confirm(message): Display a popup box with the specified message with OK and Cancel
buttons.
prompt(message, defaultValue): Display a popup box to take the user's input with the
OK and Cancel buttons.
alert()
The alert() method in JavaScript is used to display a virtual alert box. It is mostly used to
give a warning message to the users.
It displays an alert dialog box that consists of some specified message (which is optional)
and an OK button.
When the dialog box pops up, we have to click "OK" to proceed.
SYNATX:
alert(message)
EXAMPLE:
<html>
<head>
<script type = "text/javascript">
function fun() {
<body>
<p> Click the following button to see the effect </p>
<form>
<input type = "button" value = "Click me" onclick = "fun();" />
</form>
</body>
</html>
OUTPUT:
confirm()
Use the confirm() function to take the user's confirmation before starting some task. For
example, you want to take the user's confirmation before saving, updating or deleting data.
SYNATX:
bool window.confirm([message]);
EXAMPLE:
var userPreference;
Demo: confirm()
Save Data
prompt()
Use the prompt() function to take the user's input to do further actions. For example, use
the prompt() function in the scenario where you want to calculate EMI based on the user's
preferred loan tenure.
SYNATX:
string prompt([message], [defaultValue]);
EXAMPLE:
var name = prompt("Enter your name:", "John");
Demo: prompt()
Click to enter your name
ONE MARK:
1. What is JavaScript?
a) JavaScript is a scripting language used to make the website interactive
b) JavaScript is an assembly language used to make the website interactive
c) JavaScript is a compiled language used to make the website interactive
d) None of the mentioned
a) error
b) Sanfoundry_ Javascriptmcq
c) undefined
d) Sanfoundry_Javascriptmcq
a) 10
b) 50
c) 5
d) Error
a) false
b) true
c) compilation error
d) runtime error
a) false
b) true
c) compilation error
d) runtime error
12. What will be the output of the following JavaScript code snippet?
int a=1;
if(a!=null) // JavaScript not equal to Operators
return 1;
else
return 0;
a) 0
b) 1
c) compiler error
d) runtime error
13. Which of the following object is the main entry point to all client-side JavaScript features and
APIs?
a) Position
b) Window
c) Standard
d) Location
function sanfoundry(javascript)
{
return (javascript ? “yes” : “no”);
}
bool ans=true;
console.log(sanfoundry(ans));
a) Compilation error
b) Runtime error
c) Yes
d) No
a) short
b) 123.56
c) tall
d) 190
16. Which of the following can be used to call a JavaScript Code Snippet?
a) Function/Method
b) Preprocessor
c) Triggering Event
d) RMI
<p id="demo"></p>
<script>
function javascript()
{
// javacript abs() method
document.getElementById("demo").innerHTML = Math.abs(-7.25);
}
</script>
a) -7.25
b) 7.25
c) -7
d) 7
19. Which of the following explains correctly what happens when a JavaScript program is
developed on a Unix Machine?
a) will work perfectly well on a Windows Machine
b) will be displayed as JavaScript text on the browser
c) will throw errors and exceptions
d) must be restricted to a Unix Machine only
Code 2 :
var num=10;
while(num>=1)
{
document.writeln(num);
num++;
}
a) Code 1
b) Code 2
c) Both Code 1 and Code 2
d) Cannot Compare
function printArray(a)
{
var len = a.length, i = 0;
if (len == 0)
console.log("Empty Array");
else
{
// do-while loop in javascript
do
{
console.log(a[i]);
} while (++i < len);
}
}
var js = 0;
while (js < 10)
{
console.log(js);
js++;
}
a) An exception is thrown
b) The values of js are logged or stored in a particular location or storage
c) The value of js from 0 to 9 is displayed in the console
d) An error is displayed
a) 2
b) 5
c) 555
d) error
var quiz=[1,2,3];
var js=[6,7,8];
var result=quiz.concat(js);
document.writeln(result);
a) 1, 2, 3, 6, 7, 8
b) 123
c) 1, 2, 3
d) Error
27. Why JavaScript Engine is needed?
a) Both Compiling & Interpreting the JavaScript
b) Parsing the javascript
c) Interpreting the JavaScript
d) Compiling the JavaScript
int a=0;
for(a;a<5;a++);
console.log(a);
a) 4
b) 5
c) 0
d) error
30. Which of the following methods/operation does javascript use instead of == and !=?
a) JavaScript uses equalto()
b) JavaScript uses equals() and notequals() instead
c) JavaScript uses bitwise checking
d) JavaScript uses === and !== instead
31. What will be the result or type of error if p is not defined in the following JavaScript code
snippet?
console.log(p)
32. What is the prototype represents in the following JavaScript code snippet?
a) Not valid
b) Prototype of a function
c) Function javascript
d) A custom constructor
function compare()
{
int sanfoundry=1;
char javascript=1;
if(sanfoundry.tostring()===javascript)
return true;
else
return false;
}
a) runtime error
b) logical error
c) true
d) false
37. What will be the firstname and surname of the following JavaScript program?
var book = {
"main title": "JavaScript",
'sub-title': "The Definitive Guide",
"for": "all audiences",
author: {
firstname: "David",
surname: "Flanagan"
}
};
a) objects
b) property names
c) properties
d) property values
39. Consider the following JavaScript statement containing regular expressions and check if the
pattern matches.
a) text.check(pattern)
b) pattern.test(text)
c) text==pattern
d) text.equals(pattern)
5 – MARKS:
1. How to add script to your page?
2. Explain about variables?
3. Discuss about data types?
4. Explain function?
5. Discuss about alert boxes, confirm boxes?
10 – MARKS:
1. Explain about java script?
2. Discuss about operators?
3. Explain detail about message box?
4. Explain about control structures?
UNIT – 5
Working with JavaScript:
JavaScript is a client-side scripting language and one of the most efficient, commonly used
scripting languages.
The term .client-side scripting language means that it runs at the client-side( or on the client
machine) inside the web-browsers, but one important thing to remember is that client's web-
browser also needs to support the JavaScript or it must be JavaScript enabled.
Nowadays, most of the modern web browsers support JavaScript and have their JavaScript
engines. For example, Google Chrome has its own JavaScript engine called V8.
Some other web-browsers with their JavaScript engines
Web Browser JavaScript engines
1. Edge Chakra
3. Firefox Spidermonkey
It totally depends on the web-developers how they use JavaScript and for what, because it can
be used for several things in web development.
One of the most common uses of JavaScript is to validate data given by the user in the form
fields.
With the help of following example, we can understand how JavaScript works:
In this example, we have created a simple HTML document and added our JavaScript code in it.
Program
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript Working Process</title>
</head>
<body>
<h1>This how javascript works</h1>
<script>
alert("Hi,Their");
console.log("JavaScript");
</script>
</body>
/html>
In the above program, we just displayed a alert message to the user by using the "alert()"
method that is a pre-defined method of JavaScript.
We also used the "console.log();" method and passed "JavaScript" as the String and in the
inspect mode we can see "JavaScript" in the console as shown in the below output.
Output:
Practical Tips for Writing Scripts:
Read other scripts. ...
Create an outline. ...
Want to study for a master's in scriptwriting without giving up your day job? ...
Show, don't tell. ...
Keep the action moving. ...
Keep your audience in mind at all times.
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 an object-based language. Everything is an object in JavaScript.
JavaScript is template based not class based. Here, we don't create class to get the object. But, we
direct create objects.
Creating Objects in JavaScript
There are 3 ways to 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)
4.
1) JavaScript Object by object literal
The syntax of creating object using object literal is given below:
object={property1:value1,property2:value2.....propertyN:valueN}
As you can see, property and value is separated by : (colon).
simple example of creating object in JavaScript.
<script>
emp={id:102,name:"Shyam Kumar",salary:40000}
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script> Test it Now
Output
102 Shyam Kumar 40000
2) By creating instance of Object
The syntax of creating object directly is given below:
var objectname=new Object();
new keyword is used to create object.
the example of creating object directly.
<script>
var emp=new Object();
emp.id=101;
emp.name="Ravi Malik";
emp.salary=50000;
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>
Test it Now
Output
101 Ravi 50000
AD
Window Object
1.
Window Object
2.
Properties of Window Object
3.
Methods of Window Object
4.
Example of Window Object
The window object represents a window in browser. An object of window is created
automatically by the browser.
Window is the object of browser, it is not the object of javascript. The javascript objects are
string, array, date etc.
Methods of window object
The important methods of window object are as follows:
Method Description
confirm() displays the confirm dialog box containing message with ok and cancel button.
setTimeout() performs action after specified time like calling function, evaluating expressions etc.
Example of alert() in javascript
It displays alert dialog box. It has message and ok button.
<script type="text/javascript">
function msg(){
alert("Hello Alert Box");
}
</script>
<input type="button" value="click" onclick="msg()"/>
Output of the above example
<script type="text/javascript">
function msg(){
var v= confirm("Are u sure?");
if(v==true){
alert("ok");
}
else{
alert("cancel");
}
}
</script>
<input type="button" value="delete record" onclick="msg()"/>
<script type="text/javascript">
function msg(){
var v= prompt("Who are you?");
alert("I am "+v);
}
</script>
<input type="button" value="click" onclick="msg()"/>
<script type="text/javascript">
function msg(){
open("http://www.javatpoint.com");
}
</script>
<input type="button" value="javatpoint" onclick="msg()"/>
<script type="text/javascript">
function msg(){
setTimeout(
function(){
alert("Welcome to Javatpoint after 2 seconds")
},2000);
}
</script>
<input type="button" value="click" onclick="msg()"/>
Method Description
writeln("string") writes the given string on the doucment with newline character at the end.
getElementsByName() returns all the elements having the given name value.
getElementsByTagName() returns all the elements having the given tag name.
getElementsByClassName() returns all the elements having the given class name.
the simple example of document object that prints name with welcome message.
<script type="text/javascript">
function printvalue(){
var name=document.form1.name.value;
alert("Welcome: "+name);
}
</script>
<form name="form1">
Enter Name:<input type="text" name="name"/>
<input type="button" onclick="printvalue()" value="print name"/>
</form>
Enter Name:
10 mimeTypes[] returns the array of mime type. It is supported in Netscape and Firefox only.
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.
Some of the HTML events and their event handlers are:
Mouse events:
Event Performed Event Handler Description
mousedown onmousedown When the mouse button is pressed over the element
mouseup onmouseup When the mouse button is released over the element
Keyboard events:
Event Performed Event Handler Description
Keydown & Keyup onkeydown & onkeyup When the user press and then release the key
Form events:
Event Performed Event Handler Description
change onchange When the user modifies or changes the value of a form element
AD
Window/Document events
Event Performed Event Handler Description
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
OUTPUT:
MouseOver Event
<html>
<head>
<h1> Javascript Events </h1>
</head>
<body>
<script language="Javascript" type="text/Javascript">
<!--
function mouseoverevent()
{
alert("This is JavaTpoint");
}
//-->
</script>
<p onmouseover="mouseoverevent()"> Keep cursor over me</p>
</body>
</html>
OUTPUT:
Focus Event
<html>
<head> Javascript Events</head>
<body>
<h2> Enter something here</h2>
<input type="text" id="input1" onfocus="focusevent()"/>
<script>
<!--
function focusevent()
{
document.getElementById("input1").style.background=" aqua";
}
//-->
</script>
</body>
</html>
OUTPUT:
Javascript Events
Keydown Event
<html>
<head> Javascript Events</head>
<body>
<h2> Enter something here</h2>
<input type="text" id="input1" onkeydown="keydownevent()"/>
<script>
<!--
function keydownevent()
{
document.getElementById("input1");
alert("Pressed a key");
}
//-->
</script>
</body>
</html>
OUTPUT:
Javascript Events
Load event
<html>
<head>Javascript Events</head>
</br>
<body onload="window.alert('Page successfully loaded');">
<script>
<!--
document.write("The page is loaded successfully");
//-->
</script>
</body>
</html>
OUTPUT:
if (name==null || name==""){
alert("Name can't be blank");
return false;
alert("Password must be at least 6 characters long.");
return false;
}
}
</script>
<body>
<form name="myform" method="post" action="abc.jsp" onsubmit="return validateform()" >
Name: <input type="text" name="name"><br/>
Password: <input type="password" name="password"><br/>
<input type="submit" value="register">
</form>
Output:
Name:
Password:
register
if(firstpassword==secondpassword){
return true;
}
else{
alert("password must be same!");
return false;
}
}
</script>
register
Enhancements
Last Updated: 2021-03-08
Everywhere you edit scripts in the Configuration Editor, you will get a text editor, enhanced
specifically for editing JavaScript code.
This editor provides code completion, syntax coloring as well as marking syntactical errors.
Some of the enhancements are:
Code Completion
Syntax Coloring
Syntax Checking
Local Evaluation
External Editors
Code Completion
As you type, the editor performs syntax checking on the script in the background. When you have
errors in the script, it shows the error in the margins and the text will get a red squiggle under it
to mark where the JavaScript interpreter found an error.
Local Evaluation
External Editors
JavaScript Libraries :
1. React.js:
React.js is the most popular and widely used JavaScript library out there these days. It is
an open-source library.
This is sometimes considered as a framework because there are so many packages
available for reacting like handling routes which you can integrate into react.js and can
develop any type of application.
It follows SPA (Single Page Application) which is referred to as you need not refresh the
page Reactjs will automatically render the certain component and inject it into the dom
automatically.
2. jQuery:
jquery is an old javascript library that is used for DOM (Document Object Model)
manipulation and handling events like mouse clicks, scrolling, etc.
Also, it contains AJAX which handles the Asynchronous part of any application.
3. D3.js:
D3 stands for Data-Driven Documents. It is basically used for data visualization. It takes a
certain amount of data and applies it to manipulate the Document object model.
4. Underscore.js:
Underscore.js is a lightweight JavaScript library and not a complete framework that was
written by Jeremy Ashkenas that provides utility functions for a variety of use cases in our
day-to-day common programming tasks.
5. Lodash:
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in
working with arrays, strings, objects, numbers, etc.
It provides us with various inbuilt functions and uses a functional programming approach
that makes coding in JavaScript easier to understand because instead of writing repetitive
functions, tasks can be accomplished with a single line of code.
It also makes it easier to work with objects in javascript if they require a lot of
manipulation to be done upon them.
6. Anime.js:
Anime.js is a small, lightweight JavaScript library with a simple and small powerful API.
It works with the DOM element, CSS, and JavaScript object.
7. Chart.js:
Chart.js is an open-source JavaScript library on Github that allows you to draw different
types of charts by using the HTML5 canvas element. Since it uses canvas, you have to
include a polyfill to support older browsers
ONE MARK:
1. What will be printed in the console on execution of the following JS code:
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
var myArr= array.filter(v => v % 3 === 0);
console.log(myArr);
A. myArr
B. [3, 6, 9, 12, 15]
C. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
D. [1, 2, 4, 5, 7, 8, 10, 11, 13, 14]
A. [ 'J', 'v' ]
B. [ 'J' ,'v','Script']
C. [ 'J', 'v', 'Script String' ]
D. [ 'JavaScript String' ]
8. Object ......... is the process of converting an objects state to a string from which it can later be
restored.
A. prototype
B. class
C. serialization
D. extensible
9. If para1 is the DOM object for a paragraph, what is the correct syntax to change the text within
the paragraph?
A. "New Text"?
B. para1.value="New Text";
C. para1.firstChild.nodeValue= "New Text";
D. para1.nodeValue="New Text";
11. A programming language can be called object-oriented if it provides ________ basic capabilities
to developers
A. 1
B. 2
C. 3
D. 4
12. The capability to store one object inside another object known as?
A. Encapsulation
B. Aggregation
C. Inheritance
D. Polymorphism
15. JavaScript provides a special constructor function called Object() to build the object.
A. TRUE
B. FALSE
C. Can be true or false
D. Can not say
16. The type that specifies what kind of event occurred is ____________
a) event type
b) even target
c) both event type and even target
d) interface
17. Which is the object on which the event occurred or with which the event is associated?
a) event type
b) event target
c) both event type and even target
d) interface
21. The process by which the browser decides which objects to trigger event handlers on is
____________
a) Event Triggering
b) Event Listening
c) Event Handling
d) Event propagation
22. Which form of event propagation handles the registered container elements?
a) Event Propagation
b) Event Registration
c) Event Capturing
d) Default Actions
23. The events that are directly tied to a specific input device are ____________
a) Device-independent input events
b) Device-dependent input events
c) User interface events
d) State change events
24. The high-level events among the following events are ____________
a) User interface events
b) Device-independent events
c) Device-dependent events
d) Stage event change
25. The events that are not directly tied to a specific input device are _____________
a) User interface events
b) Device-independent events
c) Device-dependent events
d) Stage event change
27. What would happen if the data in the client had been wrong?
a) Sends back the data
b) Waits for correction
c) Sends back the data and Waits for correction
28. What is the purpose of the basic validation?
a) Data correctness
b) Mere data existence
c) Both Data correctness and Mere data existence
d) Data modification
32. How do you focus a particular part of the HTML page in JavaScript?
a) hover()
b) focus()
c) on()
d) focuson()
33. Which of the following is the child object of the JavaScript navigator?
a) Navicat
b) Plugins
c) NetRight
d) Plugs
40. How many constant values can the property type be represented?
a) 2
b) 3
c) 4
d) 5
5 – MARKS:
1. How to working with JavaScript?
2. Explain about window object?
3. Discuss about document object?
4. Explain browser object?
5. Discuss about JavaScript libraries?
10 – MARKS:
1. Explain about JavaScript object?
2. Discuss about JavaScript events?
3. Explain detail about validations?
4. Explain navigator object screen object?