Internet_Programming(IP)_Unit_05
Internet_Programming(IP)_Unit_05
(Unit = 5)
Java Script:
➢ Introduction to Client Side Scripting :-
Client side scripting is a process in which the code along with HTML web
page is sent to the client by the server. Here, the code refers to the script.
In other simple words, client side scripting is a process in which scripts are
executed by browsers without connecting the server.
The code executes on the browser of client’s computer either during the
loading of web page or after the web page has been loaded.
Client side scripting is mainly used for dynamic user interface elements, such
as pull-down menus, navigation tools, animation buttons, data validation
purpose, etc.
Today, it is rapidly growing and evolving day by day. As a result, writing client
side web programming is now easier and faster, thereby, reducing load on the
server.
JavaScript and jQuery are by far the most important client-side scripting
languages or web scripting languages and widely used to create a dynamic
and responsive webpage and websites.
The browser (temporarily) downloads the code in the local computer and
starts processing it without the server. Therefore, the client side scripting is
browser dependent.
A client-side script is a small program (or set of instructions) that is
embedded (or inserted) into a web page. It is processed within the client
browser instead of the web server.
The script that executes on the user’s computer system is called client. It is
embedded (or inserted) within the HTML document or can be stored in an
external separate file (known as external script).
Client side scripting is used to make web pages or website more interactive. It
is primarily used at the frontend, where the user can see using the browser.
Some important applications of client side scripting are listed, as below:
• To retrieve data from web browser or user’s screen.
• Used in the field of online games.
• To customize the web page without reloading the page.
• Client side scripting is used for validation purpose. If the user
enters incorrect credentials on the login page, the web page
displays an error message on the client machine without
submitting it to the web server.
• To create ad banners that interact with the user, rather than
simply displaying graphics.
• To create animated images that change when we move the
mouse over them.
• Client side script can be used to detect installed plug-ins and
notify the user if a plugin is required.
There are certain disadvantages of client side scripting that are as follows:
1. The main disadvantage of client side scripting is that it is unsecure because
the code is sent as is to the client and, therefore, visible to it if the client looks
at the sources of his web page. In short, code is usually visible.
2. Client side programming cannot be used if we need to access databases or
needs to transmit sensitive data over the internet.
3. There is no guarantee that user has enabled JavaScript on his computer’s
browser. Therefore, any required functionality must be loaded on the server
despite the possibility that it could be offloaded.
4. The smooth running of the script (or program) depends entirely on the
client’s browser, its configuration, and security level.
5. The web application based on the heavy JavaScript can be complicated to
debug and maintain.
6. Client side scripting languages are usually more limited in functionality than
server side scripting languages.
Features of JavaScript:
Applications of JavaScript:
• Web Development: Adding interactivity and behavior to static sites JavaScript
was invented to do this in 1995. By using AngularJS that can be achieved so
easily.
• Web Applications: With technology, browsers have improved to the extent
that a language was required to create robust web applications. When we
explore a map in Google Maps then we only need to click and drag the mouse.
All detailed view is just a click away, and this is possible only because of
JavaScript. It uses Application Programming Interfaces(APIs) that provide extra
power to the code. The Electron and React is helpful in this department.
• Server Applications: With the help of Node.js, JavaScript made its way from
client to server and node.js is the most powerful on the server-side.
• Games: Not only in websites, but JavaScript also helps in creating games for
leisure. The combination of JavaScript and HTML 5 makes JavaScript popular
in game development as well. It provides the EaseJS library which provides
solutions for working with rich graphics.
• Smartwatches: JavaScript is being used in all possible devices and
applications. It provides a library PebbleJS which is used in smartwatch
applications. This framework works for applications that require the internet
for its functioning.
• Art: Artists and designers can create whatever they want using JavaScript to
draw on HTML 5 canvas, and make the sound more effective also can be
used p5.js library.
• Machine Learning: This JavaScript ml5.js library can be used in web
development by using machine learning.
• Mobile Applications: JavaScript can also be used to build an application for
non-web contexts. The features and uses of JavaScript make it a powerful tool
for creating mobile applications. This is a Framework for building web and
mobile apps using JavaScript. Using React Native, we can build mobile
applications for different operating systems. We do not require to write code
for different systems. Write once use it anywhere!
Limitations of JavaScript:
• Security risks: JavaScript can be used to fetch data using AJAX or by
manipulating tags that load data such as <img>, <object>, <script>. These
attacks are called cross site script attacks. They inject JS that is not the part of
the site into the visitor’s browser thus fetching the details.
• Performance: JavaScript does not provide the same level of performance as
offered by many traditional languages as a complex program written in
JavaScript would be comparatively slow. But as JavaScript is used to perform
simple tasks in a browser, so performance is not considered a big restriction in
its use.
• Complexity: To master a scripting language, programmers must have a
thorough knowledge of all the programming concepts, core language objects,
client and server-side objects otherwise it would be difficult for them to write
advanced scripts using JavaScript.
• Weak error handling and type checking facilities: It is weakly typed language
as there is no need to specify the data type of the variable. So wrong type
checking is not performed by compile.
➢ Comments :-
JavaScript comments can be used to explain JavaScript code, and to make it more
readable. They can also be used to prevent the execution of a section of code if
necessary. Comments are ignored while the compiler executes the code. Comments
are user-friendly as users can get explanations of code using comments.
In javascript two types of comments are used :-
1. Single line comments
2. Multi – line comments
Any text between // and the end of the line will be ignored by JavaScript (will
not be executed).
Example:-
Multi-line Comments
Multi-line comments start with /* and end with */.
Example:-
➢ Variables in JS: -
Variables in JavaScript are containers for storing data. It is the basic unit of storage in
a program. The value stored in a variable can be changed during program execution.
A variable is only a name given to a memory location, all the operations done on the
variable effects that memory location. In JavaScript, all the variables must be
declared before they can be used. Variables can be declared in four different ways. –
• Using var
• Using let
• Using const
• Using nothing
There are some rules while declaring a JavaScript variable (also known as identifiers).
There are two types of variables in JavaScript : local variable and global variable.
➢ local variable: -
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>
➢ Global Variables: -
A variable declared outside a function is called GLOBAL variable. It can be accessed
from any function.
• Global variables are declared at the start of the block(top of the program)
• Var or let keyword is used to declare variables globally.
• Global variables can be accessed from any part of the program.
Example: -
let carName = "Volvo"; //global variable
// code here can use carName
function myFunction() {
// code here can also use carName
}
➢ Data types: -
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
JavaScript is a dynamic type language, means you don't need to specify type of the
variable because it is dynamically used by JavaScript engine. You need to
use var here to specify the data type. It can hold any type of values such as numbers,
strings etc. For example:
➢ Operators in JS: -
JavaScript operators operate the operands, these are symbols that are used to
manipulate a certain value or operand. Operators are used to performing specific
mathematical and logical computations on operands. In other words, we can say that
an operator operates the operands. In JavaScript, operators are used to comparing
values, perform arithmetic operations, etc.
Arithmetic 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
Comparison Operators
The JavaScript comparison operator compares the two operands. The comparison
operators are as follows:
Bitwise Operators: -
The bitwise operators perform bitwise operations on operands. The bitwise operators
are as follows:
Logical Operators: -
Assignment Operators: -
= Assign 10+10 = 20
Special Operators: -
(?:) Conditional Operator returns value based on the condition. It is like if-else.
• The if Statement: -
Use the if statement to specify a block of JavaScript code to be
executed if a condition is true.
Syntax: -
if (condition) {
// block of code to be executed if the condition is true
}
Example
Make a "Good day" greeting if the hour is less than 18:00:
Syntax:-
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Example
If the hour is less than 18, create a "Good day" greeting, otherwise
"Good evening":
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;
}
Example: -
<script>
var grade='B';
var result;
switch(grade){
case 'A':
result="A Grade";
break;
case 'B':
result="B Grade";
break;
case 'C':
result="C Grade";
break;
default:
result="No Grade";
}
document.write(result);
</script>
➢ Java Script Loops (For Loop, While Loop, Do While Loop): -
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.
Loops are handy, if you want to run the same code over and
over again, each time with a different value.
1. for loop
2. while loop
3. do-while loop
4. for-in loop
Syntax:
Example:
<script>
for (i=1; i<=5; i++)
{
document.write(i + "<br/>")
}
</script>
Synax:
while (condition)
{
code to be executed
}
Example:
<script>
var i=11;
while (i<=15)
{
document.write(i + "<br/>");
i++;
}
</script>
Syntax:
do{
code to be executed
}while (condition);
Example:
<script>
var i=21;
do{
document.write(i + "<br/>");
i++;
}while (i<=25);
</script>
Syntax
for (key in object) {
// code block to be executed
}
Example
const person = {fname:"John", lname:"Doe", age:25};
. This alert box will have the OK button to close the alert box.
Syntax:
alert("message");
Example
alert("I am an alert box!");
• Confirm Box: -
A confirm box is often used if you want the user to verify or accept
something.
When a confirm box pops up, the user will have to click either "OK"
or "Cancel" to proceed.
If the user clicks "OK", the box returns true. If the user clicks
"Cancel", the box returns false.
Syntax
confirm("sometext");
Example
if (confirm("Press a button!")) {
txt = "You pressed OK!";
} else {
txt = "You pressed Cancel!";
}
• Prompt Box: -
A prompt box is often used if you want the user to input a value
before entering a page.
When a prompt box pops up, the user will have to click either "OK"
or "Cancel" to proceed after entering an input value.
If the user clicks "OK" the box returns the input value. If the user
clicks "Cancel" the box returns null.
Syntax
prompt("sometext","defaultText");
Example
let person = prompt("Please enter your name", "Harry Potter");
console.log(person);
➢ JavaScript Events: -
The change in the state of an object is known as an Event. In html, there are various
events which represents that some activity is performed by the user or by the
browser. When javascript code is included in HTML, js react over these events and
allow the execution. This process of reacting over the events is called Event Handling.
Thus, js handles the HTML events via Event Handlers.
For example, when a user clicks over the browser, add js code, which will execute
the task to be performed on the event.
Mouse events:
mouseover onmouseover When the cursor of the mouse comes over the element
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:
Example
<button onclick="displayDate()">The time is?</button>
➢ JavaScript Array: -
JavaScript array is an object that represents a collection of similar type of
elements.
1. By array literal
2. By creating instance of Array directly (using new keyword)
3. By using an Array constructor (using new keyword)
1) JavaScript array literal
The syntax of creating array using array literal is given below:
var arrayname=[value1,value2.....valueN];
As you can see, values are contained inside [ ] and separated by , (comma).
➢ JS Objects: -
Objects, in JavaScript, are the most important data type and form the building blocks
for modern JavaScript. These objects are quite different from JavaScript’s primitive
data types (Number, String, Boolean, null, undefined, and symbol) in the sense that
these primitive data types all store a single value each (depending on their types).
Syntax:
new Object(value)
Object(value)
let object_name = {
key_name : value,
...
}
Note:- Object() can be called with or without new. Both create a new object.
Example:-
const o = new Object();
o.foo = 42;
console.log(o);
// { foo: 4
• Objects are more complex and each object may contain any combination of
these primitive data-types as well as reference data-types.
• An object is a reference data type. Variables that are assigned a reference
value are given a reference or a pointer to that value. That reference or
pointer points to the location in memory where the object is stored. The
variables don’t actually store the value.
• Loosely speaking, objects in JavaScript may be defined as an unordered
collection of related data, of primitive or reference types, in the form of “key:
value” pairs. These keys can be variables or functions and are called properties
and methods, respectively, in the context of an object.
An object can be created with figure brackets {…} with an optional list of properties.
A property is a “key: value” pair, where a key is a string (also called a “property
name”), and the value can be anything.
JavaScript Object Properties: The property names can be strings or numbers. In case
the property names are numbers, they must be accessed using the “bracket
notation” like this.