Oopp Unit 5 Notes
Oopp Unit 5 Notes
SYNTAX
<!DOCTYPE html>
An example of HTML document with doctype declaration.
<!DOCTYPE html>
<html>
<head>
<title>This is the title</title>
</head>
<body>
This is the content of the document.
</body>
</html>
CSS3 – the need for CSS
Cascading Style Sheets (CSS) is a style sheet language used for describing
the look and formatting of a document written in a markuplanguage.
CSS describes how HTML elements are to be displayed on screen.
CSS is a formatting language used to provide more customized web pages
and make it easier to make multiple pages use the same style.
CSS is a web development technology that stands behind the lookand-feel
of every web page.
There are three primary way to use style sheets
Inline style sheets
Embedded style sheets
Linked style sheets
SYNTAX:
EXAMPLE:
p{
color: red;
text-align: center;
font-size:12px;
}
INLINE STYLESHEET:
Inline CSS contains the CSS property in the body section attached to the
element is known as inline CSS. This kind of style is specified within an HTML
tag using the style attribute.
Syntax:
<!DOCTYPE html>
<html>
<head>
<title>Inline CSS</title>
</head>
<body>
<p style="color:green; font-size:50px;
font-style:italic; text-align:center;">
I AM A STUDENT
</p>
</body>
</html>
Output:
INTERNAL STYLESHEET:
This can be used when a single HTML document must be styled uniquely.
The CSS rule set should be within the HTML file in the head section i.e. the CSS
is embedded within the <style> tag inside the head section of the HTML file.
EXAMPLE PROGRAM:
<!DOCTYPE html>
<html>
<head>
<title>Internal CSS</title>
<style>
.main {
text-align: center;
}
.test {
color: #009900;
font-size: 50px;
font-weight: bold;
}
.testa {
font-style: bold;
font-size: 20px;
}
</style>
</head>
<body>
<div class="main">
<div class="test">HACKEREARTH</div>
<div class="testa">
A computer science portal for students
</div>
</div>
</body>
</html>
Output:
EXTERNAL CSS:
External CSS contains separate CSS files that contain only style
properties with the help of tag attributes (For example class, id, heading, … etc).
CSS property is written in a separate file with a .css extension and should be
linked to the HTML document using a link tag. It means that, for each element,
style can be set only once and will be applied across web pages.
Program:
Example:
The file given below contains CSS property. This file saves with .css extension.
For Ex: ext.css
<style>
.main {
text-align: center;
}
.test {
color: green;
font-size: 50px;
font-weight: bold;
}
.testa {
font-style: bold;
font-size: 20px;
}
</style>
Another file has to be created with link given to ext.html file.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="ext.css" />
</head>
<body>
<div class="main">
<div class="test">HACKEREARTH</div>
<div id="testa">
A CODING WEBSITE FOR STUDENTS
</div>
</div>
</body>
</html>
TYPES OF SELECTORS IN CSS:
CSS Selector
CSS selectors are used to select the content you want to style. Selectors are the
part of CSS rule set. CSS selectors select HTML elements according to its id,
class, type, attribute etc.
There are several different types of selectors in CSS.
CSS Element Selector
CSS Id Selector
CSS Class Selector
CSS Universal Selector
CSS Group Selector
CSS Element Selector (Tag Selector)
The element selector selects the HTML element by name.
EXAMPLE:
<!DOCTYPE html>
<html>
<head>
<style>
p{
text-align: center;
color: blue;
}
</style>
</head>
<body>
<p>This style will be applied on every paragraph.</p>
<p id="para1">Me too!</p>
<p>And me!</p>
</body>
</html>
OUTPUT:
CSS ID SELECTOR
The id selector selects the id attribute of an HTML element to select a
specific element. An id is always unique within the page so it is chosen to select
a single, unique element.
It is written with the hash character (#), followed by the id of the element.
EXAMPLE:
<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: blue;
}
</style>
</head>
<body>
<p id="para1">HELLOWORLD</p>
<p>This paragraph will not be affected.</p>
</body>
</html>
OUTPUT:
CSS CLASS SELECTOR
The class selector selects HTML elements with a specific class attribute. It is
used with a period character. (full stop symbol) followed by the class name.
EXAMPLE:
<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center;
color: blue;
}
</style>
</head>
<body>
<h1 class="center">This heading is blue and center-aligned.</h1>
<p class="center">This paragraph is blue and center-aligned.</p>
</body>
</html>
OUTPUT:
OUTPUT:
#childchild1 {
color: red;
}
</style>
</head>
<body>
<div id="div1">
Parent
<div id="child1">
Child 1
<div id="childchild1">
Child Child 1
<div id="childchildchild1">
Child Child Child
</div>
</div>
<div id="childchild2">
Child Child 2
</div>
</div>
<div id="child2">
Child 2
</div>
</div>
</body>
</html>
OUTPUT:
Here #parentclass has color:black, #child1 has color:green and #childchild1 has
color:red. In the above code #child1 and #child2 are in #parentclass so both
should get the color black inherited but only child 2 gets the color because we
gave #child1 to color: green this is known as specificity.
<!DOCTYPE html>
<html>
<head>
<style>
#parentclass {
padding: 30px;
color: red;
}
#Child {
padding: inherit;
}
</style>
</head>
<body>
<div id="parentclass">
Parent
<div id="Child">Child</div>
</div>
</body>
</html>
OUTPUT:
JAVASCRIPT VARIABLES:
A JavaScript variable is simply a name of storage location. There are two
types of variables in JavaScript : local variable and global variable.
There are some rules while declaring a JavaScript variable (also known as
identifiers).
RULES:
Name must start with a letter (a to z or A to Z), underscore( _ ), or dollar( $ )
sign.
After first letter we can use digits (0 to 9), for example value1.
JavaScript variables are case sensitive, for example x and X are different
variables.
Correct JavaScript variables
var x = 10;
var _value="sonoo";
Incorrect JavaScript variables
var 123=30;
var *aa=320;
EXAMPLE:
<script>
var x = 10;
var y = 20;
var z=x+y;
document.write(z);
</script>
OUTPUT: 30
JAVASCRIPT FUNCTIONS:
A JavaScript function is a block of code designed to perform a particular
task.
A JavaScript function is executed when "something" invokes it (calls it).
A JavaScript function is defined with the function keyword, followed by a
name, followed by parentheses ().
Function names can contain letters, digits, underscores, and dollar signs
(same rules as variables).
The parentheses may include parameter names separated by commas:
(parameter1, parameter2, ...)
The code to be executed, by the function, is placed inside curly brackets: {}
SYNTAX:
function name(parameter1, parameter2, parameter3) {
// code to be executed
}
EXAMPLE:
function myFunction(p1, p2) {
return p1 * p2;
}
Function Invocation
The code inside the function will execute when "something" invokes (calls)
the function:
When an event occurs (when a user clicks a button)
When it is invoked (called) from JavaScript code
Automatically (self invoked)
Function Return
When JavaScript reaches a return statement, the function will stop
executing.
EXAMPLE:
// Function is called, the return value will end up in x
let x = myFunction(4, 3);
function myFunction(a, b) {
// Function returns the product of a and b
return a * b; }
JAVASCRIPT OBJECTS:
BUILT-IN OBJECTS:
In JavaScript, almost "everything" is an object.
Booleans can be objects (if defined with the new keyword)
Numbers can be objects (if defined with the new keyword)
Strings can be objects (if defined with the new keyword)
Date are always objects
Math are always objects
Regular expressions are always objects
Arrays are always objects
Functions are always objects
Objects are always objects
Array Object
Multiple values are stored in a single variable using the Array object.
In JavaScript, an array can hold different types of data types in a single
slot, which implies that an array can have a string, a number or an object
in a single slot.
An Array object can be created by using following ways:
Using the Array Constructor:
To create empty array when don‟t know the exact number of elements to be
inserted in an array
var arrayname = new Array();
To create an array of given size
var arrayname = new Array(size);
To create an array with given elements
var arrayname = new Array(“element 1”,”element 2”,……..,”element n”);
Using the Array Literal Notation:
To create empty array
var arrayname =[ ];
To create an array when elements are given
var arrayname =[“element 1”,”element 2”,……..,”element n”];
Properties of the Array object
o Length - Returns the number of elements in the array.
o Constructor - Returns the function that created the Array object.
o Prototype - Add properties and methods to an object.
Methods of the Array object
o reverse() - Reverses the array elements
o concat() - Joins two or more arrays
o sort() - Sort the elements of an array
o push() - Appends one or more elements at the end of an array
o pop() - Removes and returns the last element
o shift() - Removes and returns the first element
unshift(), join(), indexOf(), lastIndexOf(), slice(startindex, endindex) are
some of the methods used in Array object.
Boolean Object
The Boolean object is wrapper class and member of global objects.
It is used to convert a non-Boolean value to a Boolean value (true or
false).
If the Boolean object has no initial value or if it is 0, -0, null, "", false,
undefined, or NaN, the object is set to false. Otherwise it is true (even with
the string "false")!
Boolean object can be created by using following ways:
Using Boolean Literal Notation: var bool = true;
Using Boolean Object as Function: var bool = Boolean(true);
Using Testable Expressions: if(true) {………….}
Properties of the Boolean object
o Constructor - Returns the function that created the Boolean object.
o Prototype - Add properties and methods to an object.
Methods of the Boolean object
o toSource() - Returns a string containing the source of the Boolean
object; you can use this string to create an equivalent object.
o toString() - Returns a string of either "true" or "false" depending
upon the value of the object.
o valueOf() - Returns the primitive value of the Boolean object
Date Object
At times when user need to access the current date and time and also
past and future date and times. JavaScript provides support for working
with dates and time through the Date object.
The Date object provides a system-independent abstraction of dates and
times.
Date object can be created as : var today = new Date( );
Dates may be constructed from a year, month, day of the month, hour,
minute, and second, and those six components, as well as the day of the
week, may be extracted from a date.
Dates may also be compared and converted to a readable string form. A
Date is represented to a precision of one millisecond.
Properties of Date object
o Constructor - Returns the function that created the Date object.
o Prototype - Add properties and methods to an object.
Methods of Date object
o Date() - Returns today's date and time
o getDate() - Returns the day of the month for the specified date
o getDay() - Returns the day of the week for the specified date
o getFullYear() - Returns the year of the specified date
o getHours() - Returns the hour in the specified date according to
local time.
o getMilliseconds() - Returns the milliseconds in the specified date
according to local time.
getMinute(), getMonth(), getTime(), getTimezoneOffset(), setDate(),
setFullYear(), setHours(), setMilliseconds(), setMinutes(), setMonth(),
setSeconds(), setTime() are some of the methods used in Date object.
Math Object
The Math object is used to perform simple and complex arithmetic
operations.
The Math object provides a number of properties and methods to work
with Number values
The Math object does not have any constructors. All of its methods and
properties are static; that is, they are member functions of the Math
object itself. There is no way to create an instance of the Math object.
Properties of Math object
o PI - The value of Pi
o E - The base of natural logarithm e
o LN2 - Natural logarithm of 2
o LN10 - Natural logarithm of 10
o LOG2E - Base 2 logarithm of e
o LOG10E - Base 10 logarithm of e
o SQRT2 - Square root of 2
o SQRT1_2 - Square root of ½
Methods of Math object
o max(a,b) - Returns largest of a and b
o min(a,b) - Returns least of a and b
o round(a) - Returns nearest integer
o ceil(a) - Rounds up. Returns the smallest integer greater than or
equal to a
o floor(a) - Rounds down. Returns the largest integer smaller than or
equal to a
o exp(a) - Returns ea
o pow(a,b) - Returns ab
o abs(a) - Returns absolute value of a
o random() - Returns a pseudo random number between 0 and 1
o sqrt(a) - Returns square root of a
o sin(a) - Returns sin of a (a is in radians)
o cos(a) - Returns cos of a (a is in radians)
Number Object
The Number objects represents numerical date, either integers or floating-
point numbers.
A Number objects are created using the Number() constructor var num =
new number(value);
Properties of Number object
o Constructor - Returns the function that created the Number object.
o MAX VALUE - Returns maximum numerical value possible in
JavaScript.
o MIN VALUE - Returns minimum numerical value possible in
JavaScript.
o NEGATIVE INFINITY - Represent the value of negative infinity.
o POSITIVE INFINITY - Represent the value of infinity.
o Prototype - Add properties and methods to an object.
Methods of Number object
o toExponential() - Converts a number into exponential notation.
o toFixed() - Formats a number with a specific number of digits to
the right of the decimal.
o toLocaleString() - Returns a string value version of the current
number in a format that may vary according to a browser's locale
settings.
o toPrecision() - Defines how many total digits to display of a
number.
o toString() - Returns the string representation of the number's
value.
o valueOf() - Returns the number's value.
String Object
in JavaScript, all strings are represented as instances of the String object.
The String object is wrapper class and member of global objects.
String object used to perform operations on the stored text, such as
finding the length of the string, searching for occurrence of certain
characters within string, extracting a substring etc.
A String is created by using literals. A string literal is either enclosed
within single quotes(„ „) or double quotes(“ “) var string1= “ Ques10“ ;
Properties of String object
o Length - Returns the length of string.
o Constructor - Returns the function that created the String object
o Prototype - Add properties and methods to an object
Methods of String object
o charAt() - Returns the character at a specified position.
o concat() - Combines two or more strings.
o toLowerString() - Converts a string to lowercase.
o toUpperString() - Converts a string to uppercase.
o indexOf(searchtext, index) - Searches for the specified string from
the beginning of the string.
o lastIndexof(searchtext, index) - Searches for the specified string
from the end of the string
The HTML DOM can be accessed with JavaScript (and with other programming
languages).
A property is a value that you can get or set (like changing the content of an
HTML element).
Example
The following example changes the content (the innerHTML) of
the <p> element with id="demo":
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello World!";
</script>
</body>
</html>
In the example above, getElementById is a method, while innerHTML is
a property.
The getElementById Method
The most common way to access an HTML element is to use the id of the
element.
In the example above the getElementById method used id="demo" to find the
element.
The innerHTML Property
The easiest way to get the content of an element is by using
the innerHTML property.
The innerHTML property is useful for getting or replacing the content of HTML
elements.
The innerHTML property can be used to get or change any HTML element,
including <html> and <body>.
The HTML DOM document object is the owner of all other objects in your web
page.
The HTML DOM Document Object
The document object represents your web page.
If you want to access any element in an HTML page, you always start with
accessing the document object.
Below are some examples of how you can use the document object to access and
manipulate HTML.
Finding HTML Elements
This example changes the value of the src attribute of an <img> element:
Example
<!DOCTYPE html>
<html>
<body>
<img id="myImage" src="smiley.gif">
<script>
document.getElementById("myImage").src = "landscape.jpg";
</script>
</body>
</html>
Example explained:
The HTML document above contains an <img> element with id="myImage"
We use the HTML DOM to get the element with id="myImage"
A JavaScript changes the src attribute of that element from "smiley.gif" to
"landscape.jpg"
Syntax:
<HTML-element Event-Type = "Action to be performed">
These are some javascript events:
JavaScript onclick events: This is a mouse event and provokes any logic
defined if the user clicks on the element it is bound to.
Example: In this example, we will display a message in the alert box when the
button is clicked
<!doctype html>
<html>
<head>
<script>
function hiThere() {
alert('Hi there!');
}
</script>
</head>
<body>
<button type="button"
onclick="hiThere()"
style="margin-left: 50%;">
Click me event
</button>
</body>
</html>
It is important to validate the form submitted by the user because it can have
inappropriate values. So, validation is must to authenticate user.
JavaScript provides facility to validate the form on the client-side so data processing will
be faster than server-side validation. Most of the web developers prefer JavaScript form
validation.
Through JavaScript, we can validate name, password, email, date, mobile numbers and
more fields.
if (name==null || name==""){
alert("Name can't be blank");
return false;
}else if(password.length<6){
alert("Password must be at least 6 characters long.");
return false;
}
}
</script>
<body>
<form name="myform" method="post" action =
http://www.javatpoint.com/javascriptpages/valid.jsp
onsubmit="return validateform()" >
Name: <input type="text" name="name"><br/>
Password: <input type="password" name="password"><br/>
<input type="submit" value="register">
</form>
</body>
</html>
OUTPUT: