css chapter 1
css chapter 1
A script is a small piece of program that can add interactivity to the website. For example, a
script could generate a pop-up alert box message, or provide a dropdown menu. This script
could be JavaScript or VBScript.
A script language is a simple programming language with which you can write scripts. A
scripting language is a form of programming language that is usually interpreted rather than
compiled.
Conventional programs are converted permanently into executable files before they are run.
In contrast, programs in scripting language are interpreted one command at a time. Scripting
languages are often written to facilitate enhanced features of Web sites.
JavaScript, Perl, PHP, Python, VBScript are the most popular examples of scripting
Languages.
Client side scripts may also have some instructions for the web browser to follow in response to
certain user actions, such as pressing a page button. They can often be looked if client want to
view the source code of web page.
Features of JavaScript
JavaScript is a lightweight, interpreted programming language.
Interpreter Based Scripting Language
Case Sensitive
Designed for creating network-centric applications.
Integrated with HTML.
Open and cross-platform
Validating User’s Input : JavaScript is very useful while using forms. It has the capability to
validate user input for errors and also saves time. If the user leaves a required field empty or
the information is incorrect, JavaScript checks for them before sending the data over to the
server.
Advantages of JavaScript
Speed: Client-side JavaScript is very fast because it can be run immediately within the
client-side browser.
Simplicity: JavaScript is relatively simple to learn and implement.
Popularity: JavaScript is used everywhere on the web.
Interoperability: JavaScript plays nicely with other languages and can be used in a huge
variety of applications.
Server Load: Being client-side reduces the demand on the website server.
Gives the ability to create rich interfaces.
Less server interaction − You can validate user input before sending the page off to the
server. This saves server traffic, which means less load on your server.
Immediate feedback to the visitors
Increased interactivity − You can create interfaces that react when the user hovers
over them with a mouse or activates them via the keyboard.
JavaScript Programming
Q] Explain <script> tag with example?
JavaScript can be implemented using JavaScript statements that are placed within
the <script>... </script> HTML tags in a web page.
You can place the <script> tags, containing your JavaScript, anywhere within your web page,
but it is normally recommended that you should keep it within the <head> tags.
The <script> tag alerts the browser program to start interpreting all the text between these
tags as a script. A simple syntax of your JavaScript will appear as follows.
<script ...>
JavaScript code
</script>
The script tag takes two important attributes −
1. Language − This attribute specifies what scripting language you are using. Typically, its
value will be javascript. Although recent versions of HTML (and XHTML, its successor)
have phased out the use of this attribute.
2. Type – This attribute tells the browser that script is in plain text and text is organised in
the format of javascript.
So your JavaScript segment will look like −
<script language = "javascript" type = "text/javascript">
JavaScript code
</script>
But when formatted in a single line as follows, you must use semicolons −
<script language = "javascript" type = "text/javascript">
document.write("Hello"); document.write("Welcome to Arrow Academy")
</script>
Case Sensitivity
JavaScript is a case-sensitive language. This means that the language keywords, variables,
function names, and any other identifiers must always be typed with a consistent capitalization
of letters.
So the identifiers Time and TIME will convey different meanings in JavaScript.
Comments in JavaScript
JavaScript supports both C-style and C++-style comments, Thus −
Any text between a // and the end of a line is treated as a comment and is ignored by
JavaScript.
Any text between the characters /* and */ is treated as a comment. This may span
multiple lines.
Example
The following example shows how to use comments in JavaScript.
<script language = "javascript" type = "text/javascript">
// This is a comment. It is similar to comments in C++
/*
This is a multi-line comment in JavaScript
It is very similar to comments in C Programming
*/
</script>
JavaScript Placement
There is a flexibility given to include JavaScript code anywhere in an HTML document. However
the most preferred ways to include JavaScript in an HTML file are as follows −
Script in <head>...</head> section.
Script in <body>...</body> section.
Script in <body>...</body> and <head>...</head> sections.
Script in an external file and then include in <head>...</head> section.
In the following section, we will see how we can place JavaScript in an HTML file in different
ways.
<body>
document.write("Hello World")
</script>
</body>
</html>
function sayHello()
{
alert("Welcome to Arrow World")
}
</script>
</head>
<body>
document.write("Hello World")
</script>
<body>
<input type = "button" onclick = "sayHello()" value = "Say
Hello" />
</body>
</html>
To use JavaScript from an external file source, you need to write all your JavaScript source code
in a simple text file with the extension ".js" and then include that file as shown above. For
example, you can keep the following content in filename.js file and then you can
use sayHello function in your HTML file after including the filename.js file.
function sayHello()
{
alert("Welcome to Arrow")
}
document.write("I am creating javascript external code<br>");
Variable Declaration:
Before you use a variable in a JavaScript program, you must declare it.
Variables are declared with the var keyword as follows.
You can also declare multiple variables with the same var keyword as follows −
Variable Initialization:
Storing a value in a variable is called variable initialization.
You can do variable initialization at the time of variable creation or at a later point in time when
you need that variable.
</script>
Note − Use the var keyword only for declaration or initialization, once for the life of any
variable name in a document. You should not re-declare same variable twice.
JavaScript is untyped language. This means that a JavaScript variable can hold a value of any
data type. Unlike many other languages, you don't have to tell JavaScript during variable
declaration what type of value the variable will hold. The value type of a variable can change
during the execution of a program and JavaScript takes care of it automatically.
Global Variables − A global variable has global scope which means it can be defined anywhere
in your JavaScript code.
Local Variables − A local variable will be visible only within a function where it is defined.
Within the body of a function, a local variable takes precedence over a global variable with the
same name.
If you declare a local variable or function parameter with the same name as a global variable,
you effectively hide the global variable.
Example:
<script type = "text/javascript">
1. You should not use any of the JavaScript reserved keywords as a variable name. For example,
break or boolean variable names are not valid.
3. JavaScript variable names are case-sensitive. For example, Name and name are two different
variables.
JavaScript provides different data types to hold different types of values. There are two types of
data types in JavaScript.
1. Primitive data type
2. Non-primitive (reference) data type
There are five types of primitive data types in JavaScript. They are as follows:
All cars have the same properties, but the property values differ from car to car. All cars have
the same methods, but the methods are performed at different times.
A] JavaScript Objects
B] Object Name
Each object is uniquely identified by a name or ID.
A web page contains many objects, some of which are the same kind of object. These objects
can be easily distinguishable by using its name or ID.
C] Property
A property is a data or value associated with an object.
Objects can have many data's associated with them depending on application of use.
For example, a form object can have a title, a width and a height as properties whereas a
window object can have a background color, a width and height as properties.
With JavaScript, you can define and create your own objects. There are different ways to create
new objects:
This is the easiest way to create a JavaScript Object. Using an object literal, you both define and
create an object in one statement. An object literal is a list of name:value pairs inside curly
braces {}.
The following example creates a new JavaScript object with four properties:
syntax :
object={property1:value1,property2:value2.....propertyN:valueN}
As you can see, property and value is separated by : (colon).
Example:
<script>
emp={id:102,name:"Shyam Kumar",salary:40000}
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>
Spaces and line breaks are not important. An object definition can span multiple lines:
<script>
emp= {
id:102,
name:"Shyam Kumar",
salary:40000
}
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>
Syntax:
E] Dot Syntax:
The properties & methods associated with any object can be accessed by using the object
name with dot syntax.
E.g. user.fname, user.lname & user.fullName( ).
Example:
<html>
<head>
<title>Object</title>
</head>
<body>
<script>
var user = {
F] Method
A method is a set of statements (actions) performed by an object when it receives a message.
On a form when you click on Submit button, the form is submitted to server side application.
Here Submit is an object, and clicking on it causes the button to process a method.
Example:
<script>
var person = {
firstName: "John",
lastName : "Doe",
fullName : function()
{
return this.firstName + " " + this.lastName;
}
};
document.write(“User Name is:” + person.fullname());
</script>
Arithmetic Operators
Comparison Operators
Logical (or Relational) Operators
Assignment Operators
Conditional (or ternary) Operators
Lets have a look on all operators one by one.
1) Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on the operands.
Assume variable A holds 10 and variable B holds 20, then −
Sr. Operator & Description Example
2 - (Subtraction): Subtracts the second operand from the first Ex: A - B will give -10
= (Simple Assignment )
Ex: C = A + B will assign
1 Assigns values from the right side operand to the left side
the value of A + B into C
operand
= = (Equal)
1 Checks if the value of two operands are equal or not, if yes, then the Ex: (A == B) is not true.
condition becomes true.
!= (Not Equal)
2 Checks if the value of two operands are equal or not, if the values Ex: (A != B) is true.
are not equal, then the condition becomes true.
|| (Logical OR)
2 If any of its arguments are true, it returns true, otherwise it Ex: (A>B || B < C) is false.
returns false.
! (Logical NOT)
3 If a condition is true, then the Logical NOT operator will Ex: ! (A && B) is false.
make it false and vice versa.
5) Bitwise Operators
Assume variable A holds 2 and variable B holds 3, then −
Sr. Operator & Description Example
| (BitWise OR)
2 It performs a Boolean OR operation on each bit of its integer Ex: (A | B) is 3.
arguments.
^ (Bitwise XOR)
It performs a Boolean exclusive OR operation on each bit of its integer
3 Ex: (A ^ B) is 1.
arguments. Exclusive OR means that either operand one is true or
operand two is true, but not both.
~ (Bitwise Not)
4 It is a unary operator and operates by reversing all the bits in the Ex: (~B) is -4.
operand.
Conditional Statements
While writing a program, there may be a situation when you need to adopt one out of a given
set of paths. In such cases, you need to use conditional statements that allow your program to
make correct decisions and perform right actions.
JavaScript supports conditional statements which are used to perform different actions based
on different conditions.
A] if..else statement
Flow Chart of if-else
Syntax
if (expression)
{
Statement(s) to be executed if expression is true
}
Here a JavaScript expression is evaluated. If the resulting value is true, the given statement(s)
are executed. If the expression is false, then no statement would be executed. Most of the
times, you will use comparison operators while making decisions.
Example
Try the following example to understand how the if statement works.
<html>
<body>
<script type = "text/javascript">
var age = 20;
if( age > 18 )
{
document.write("Qualifies for driving ");
}
</script>
</body>
</html>
Output
Qualifies for driving
2) if...else statement
Syntax
if (expression)
{
Statement(s) to be executed if expression is true
}
else
{
Statement(s) to be executed if expression is false
}
Here JavaScript expression is evaluated. If the resulting value is true, the given statement(s) in
the ‘if’ block, are executed. If the expression is false, then the given statement(s) in the else
block are executed.
Try the following code to learn how to implement an if-else statement in JavaScript.
<html>
<body>
<script type = "text/javascript">
var age = 15;
Output
Does not qualify for driving
The if...else if... statement is an advanced form of if…else that allows JavaScript to make a correct
decision out of several conditions.
Syntax
The syntax of an if-else-if statement is as follows −
if (expression 1)
{
Statement(s) to be executed if expression 1 is true
}
else if (expression 2)
{
Statement(s) to be executed if expression 2 is true
}
else if (expression 3)
{
Statement(s) to be executed if expression 3 is true
}
else
{
Statement(s) to be executed if no expression is true
}
It is just a series of if statements, where each if is a part of the else clause of the previous
statement. Statement(s) are executed based on the true condition, if none of the conditions is
true, then the else block is executed.
<!DOCTYPE html>
<html>
<head>
<title>First Page</title>
<script type = "text/javascript">
var m1=30,m2=40,m3=35,m4=34,m5=30;
var total=m1+m2+m3+m4+m5;
var per=total/5
document.write("Total is "+total)
document.write("<br>")
document.write("Percentage is "+per)
document.write("<br>")
if(per>70)
{
document.write("Distinction")
}
else if(per>60)
{
document.write("First Class")
}
else if(per>50)
{
document.write("Second Class")
}
else if(per>40)
{
document.write("Pass")
}
else
{
document.write("Fail")
}
</script>
</head>
<body>
</body>
</html>
Example:
<!DOCTYPE html>
<html>
<head>
<title>Nested If Else</title>
<script type="text/javascript">
var a=10,b=40,c=30;
if(a>b)
{
if(a>c)
{
document.write("Largest number is "+a)
}
else
{
document.write("largest number is "+c)
}
}
else
{
if(b>c)
{
document.write("Largest number is "+b)
}
else
{
document.write("Largest number is "+c)
}
}
</script>
</head>
<body>
</body>
</html>
Notes:
The break statement is optional. If the break statement is encountered, the switch statement
ends.
If the break statement is not used, the cases after the matching case are also executed.
The default clause is also optional.
Flowchart:
default:
document.write("Wrong Arithmetic Operator")
break;
}
</script>
</head>
<body>
</body>
</html>
while (expression)
{
Statement(s) to be executed if expression is true
}
Flow Chart
<html>
<body>
document.write("Loop stopped!");
</script>
Output
Starting Loop
Current Count : 0
Current Count : 1
Current Count : 2
Current Count : 3
Current Count : 4
Current Count : 5
Current Count : 6
Current Count : 7
Current Count : 8
Current Count : 9
Loop stopped!
Set the variable to different value and then try...
Syntax
do
{
Statement(s) to be executed;
} while (expression);
Note − Don’t miss the semicolon used at the end of the do...while loop.
Example:
Try the following example to learn how to implement a do-while loop in JavaScript.
<html>
<body>
<script type = "text/javascript">
<!--
var count = 0;
Output
Starting Loop
Current Count : 0
Current Count : 1
Current Count : 2
Current Count : 3
Current Count : 4
Loop Stopped!
Set the variable to different value and then try...
Syntax
for (initialization; test condition; iteration statement)
{
Statement(s) to be executed if test condition is true
}
<html>
<body>
<script type = "text/javascript">
var count;
document.write("Starting Loop" + "<br />");
Output
Starting Loop
Current Count : 0
Current Count : 1
Current Count : 2
Current Count : 3
Current Count : 4
Current Count : 5
Current Count : 6
Current Count : 7
Current Count : 8
Current Count : 9
Loop stopped!
Set the variable to different value and then try...
Output:
Starting Loop
Current Count : 0
Current Count : 1
Current Count : 2
Current Count : 3
Current Count : 4
continue statement
The continue statement in Java is used to skip the current iteration of a loop.
<script type="text/javascript">
var count;
document.write("Starting Loop" + "<br />");
}
</script>
1. Array literals
In Javascript, an array literal is a list of expressions, each of which represents an array element,
enclosed in a pair of square brackets ' [ ] ' . When an array is created using an array literal, it is
initialized with the specified values as its elements, and its length is set to the number of
arguments specified. If no value is supplied it creates an empty array with zero length.
2. Integers literals
An integer must have at least one digit (0-9).
No comma or blanks are allowed within an integer.
It does not contain any fractional part.
It can be either positive or negative if no sign precedes it is assumed to be positive.
In JavaScript, integers can be expressed in three different bases.
3. Octal (base 8)
Octal numbers can be made with the digits 0, 1, 2, 3, 4, 5, 6, 7. A leading 0 indicates the number
is octal.
Example: 0123
5. Object literals
An object literal is zero or more pairs of comma-separated list of property names and
associated values, enclosed by a pair of curly braces.
In JavaScript an object literal is declared as follows:
Syntax Rules
There is a colon (:) between property name and value.
A comma separates each property name/value from the next.
There will be no comma after the last property name/value pair.
6. String literals
A string literal is zero or more characters, either enclosed in single quotation (') marks or
double quotation (") marks.
You can also use + operator to join strings.
The following are the examples of string literals :
string1 = "computer"
string1 = 'engineering.com'
string1 = "1000"
string1 = "google" + ".com"