Unit 3
Unit 3
BCS 502
Unit 3 - Notes
Unit 3 - Notes
Scripting: Java script: Introduction, documents, forms, statements, functions, objects; introduction to AJAX.
Networking : Internet Addressing, InetAddress, Factory Methods, Instance Methods, TCP/IP Client Sockets, URL, URL
Connection, TCP/IP Server Sockets, Datagram.
What is JavaScript
3. It was introduced in the year 1995 for adding programs to the web pages in the Netscape
Navigator browser. Since then, it has been adopted by all other graphical web browsers.
4. With JavaScript, users can build modern web applications to interact directly without
reloading the page every time. The traditional website uses js to provide several forms of
Features of JavaScript
1. All popular web browsers support JavaScript as they provide built-in execution
environments.
2. JavaScript follows the syntax and structure of the C programming language. Thus, it is a
3. JavaScript is a weakly typed language, where certain types are implicitly cast (depending
on the operation).
6. It is a case-sensitive language.
History of JavaScript
In 1993, Mosaic, the first popular web browser, came into existence. In the year
1994, Netscape was founded by Marc Andreessen. He realized that the web needed to become
more dynamic. Thus, a 'glue language' was believed to be provided to HTML to make web
Consequently, in 1995, the company recruited Brendan Eich intending to implement and embed
Scheme programming language to the browser. But, before Brendan could start, the company
merged with Sun Microsystems for adding Java into its Navigator so that it could compete with
Microsoft over the web technologies and platforms. Now, two languages were there: Java and
the scripting language. Further, Netscape decided to give a similar name to the scripting
language as Java's. It led to 'Javascript'. Finally, in May 1995, Marc Andreessen coined the first
code of Javascript named 'Mocha'. Later, the marketing team replaced the name with
'LiveScript'. But, due to trademark reasons and certain other reasons, in December 1995, the
language was finally renamed to 'JavaScript'. From then, JavaScript came into existence.
Application of JavaScript
o Client-side validation,
o Displaying pop-up windows and dialog boxes (like an alert dialog box, confirm dialog
Example of JavaScript
<html>
<body>
<h2>Welcome to JavaScript</h2>
<script>//Start tag
document.write("Hello JavaScript");
</script>//End Tag
</body>
</html>
Difference Between Java and javaScript
→ JavaScript Document object is an object that provides access to all HTML elements of a
document. When an HTML document is loaded into a browser window, then it becomes a
document object.
→ The document object stores the elements of an HTML document, such as HTML, HEAD,
→ A document object is a child object of the Window object, which refers to the browser.
→ You can access a document object either using window.document property or using object
Directly
1. Local variable
2. Global variable.
There are some rules while declaring a JavaScript variable (also known as identifiers).
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";
<html>
<body>
<script>
var x = 10;
var y = 20;
var z=x+y;
document.write(z);
</script>
</body>
</html>
A JavaScript local variable is declared inside block or function. It is accessible within the
<script>
function abc(){
</script>
the function or declared with window object is known as global variable. For example:
<html>
<body>
<script>
function a(){
document.writeln(data); }
function b(){
document.writeln(data); }
b();
</script>
Data Types
JavaScript provides different data types to hold different types of values. There are two types of
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:
There are five types of primitive data types in JavaScript. They are as follows:
JavaScript non-primitive data types
JavaScript Operators
JavaScript operators are symbols that are used to perform operations on operands.
For example:
var sum=10+20;
1. Arithmetic Operators
3. Bitwise Operators
4. Logical Operators
5. Assignment Operators
6. Special Operators
1. Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on the operands. The following
The JavaScript comparison operator compares the two operands. The comparison operators are
as follows:
3. Bitwise Operators
The bitwise operators perform bitwise operations on operands. The bitwise operators are as
follows:
4.Logical Operators
5. Assignment Operators
6. Special Operators
6. Special Operators
The JavaScript if-else statement is used to execute the code whether condition is true or false.
1. If Statement
2. If else statement
3. if else if statement
If Statement
<script>
var a=20;
if(a>10){
</script>
It evaluates the content whether condition is true of false. The syntax of JavaScript if-else
if(expression){
else{
It evaluates the content only if expression is true from several expressions. The signature of
<script>
var a=20;
if(a==10){
else if(a==15){
else if(a==20){
document.write("a is equal to 20"); }
else{
</script>
JavaScript Loops
The JavaScript loops are used to iterate the piece of code using for, while, do while loops. It
1. for loop
2. while loop
3. do-while loop
The JavaScript for loop iterates the elements for the fixed number of times. It should be used if
document.write(i + "<br/>")
The JavaScript while loop iterates the elements for the infinite number of times. It should be
used if number of iteration is not known. The syntax of while loop is given below.
while (condition)
code to be executed
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
do{
code to be executed
}while (condition);
JavaScript Functions
JavaScript functions are used to perform operations. We can call JavaScript function many
2. Less coding: It makes our program compact. We don’t need to write many lines of code
//code to be executed
JavaScript Objects
A JavaScript object is an entity having state and behavior (properties and method). For example:
JavaScript is template based not class based. Here, we don't create class to get the object. But, we
1. By object literal
The syntax:
object={
property1:value1,property2:value2.....propertyN:valueN
emp={
id:102,name:"Shyam Kumar",salary:40000
</script>
Output:
<script>
emp.id=101;
emp.name="Ravi Malik";
emp.salary=50000;
</script>
Here, you need to create function with arguments. Each argument value can be assigned in the
<script>
function emp(id,name,salary){
this.id=id;
this.name=name;
this.salary=salary;
</script>
Output:
JavaScript Array
1. By array literal
var arrayname=[value1,value2.....valueN];
Let's see the simple example of creating and using array in JavaScript.
<script>
var emp=["Sonoo","Vimal","Ratan"];
for (i=0;i<emp.length;i++){
document.write(emp[i] + "<br/>");
</script>
<script>
var i;
emp[0] = "Arun";
emp[1] = "Varun";
emp[2] = "John";
for (i=0;i<emp.length;i++){
document.write(emp[i] + "<br>");
</script>
Here, you need to create instance of array by passing arguments in constructor so that we don't
<script>
for (i=0;i<emp.length;i++){
document.write(emp[i] + "<br>");
</script>
Array Properties
Here is a list of the properties of the Array object along with their description.
Introduction to Forms
Forms are the basics of HTML. We use HTML form element in order to create
the JavaScript form. For creating a form, we can use the following sample code:
<html>
<head>
</head>
<body>
<h4> USERNAME</h4>
<h4> PASSWORD</h4>
</form>
</html>
In the code:
o Form name tag is used to define the name of the form. The name of the form here is
o The action tag defines the action, and the browser will take to tackle the form when it is
o The method to take action can be either post or get, which is used when the form is to be
submitted to the server. Both types of methods have their own properties and rules.
o The input type tag defines the type of inputs we want to create in our form. Here, we have
used input type as 'text', which means we will input values as text in the textbox.
o Net, we have taken input type as 'password' and the input value will be password.
o Next, we have taken input type as 'button' where on clicking, we get the value of the form
Other than action and methods, there are the following useful methods also which are provided
Introduction to AJAX
Introduction to AJAX:-
AJAX stands Asynchronous, Javascript and XML. Asynchronous means that we are exchanging data to from the Serven in
the background without having to refresh the page again and again group of inter related web development technique
used on the client side to create web application.
AJAX is not a programming language rather AJAX is a technique used by web developer inorder to make website behave
like desktop applications.
➔AJAX is Based on Internet StandardsAJAX is based on internet standards, and uses a combination of:
➔CSS (to style the data)XML (often used as the format for transferring data)AJAX applications are browser- and
platform-independent
Why use Ajax?
Mainly to build a fast, dynamic website, but also to save resources.
For improving sharing of resources, it is better to use the power of all the clientcomputers rather than just a unique
server and network.
Ajax allows to perform processing on client computer (in JavaScript) with datataken from the server. The processing of
web page formerly was only server-side,using web services or PHP scripts, before the whole page was sent within
thenetwork.
But Ajax can selectively modify a part of a page displayed by the browser, andupdate it without the need to reload the
whole document with all images, menus,etc...
For example, fields of forms, choices of user, may be processed and the resultdisplayed immediately into the same page
1. User sends a request from the UI and a javascript call goes to XMLHttpRequest object.
3. Server interacts with the database using JSP, PHP, Servlet, ASP.net etc.
4. Data is retrieved.
5. Server sends XML data or JSON data to the XMLHttpRequest callback function.
IP Address :
Classful Addressing
The 32-bit IP address is divided into five sub-classes. These are given below:
• Class A
• Class B
• Class C
• Class D
• Class E
Each of these classes has a valid range of IP addresses. Classes D and E are reserved for multicast and experimental
purposes respectively. The order of bits in the first octet determines the classes of the IP address.
• IP addresses are globally managed by Internet Assigned Numbers Authority(IANA) and Regional Internet
Registries(RIR).
• While finding the total number of host IP addresses, 2 IP addresses are not counted and are therefore,
decreased from the total count because the first IP address of any network is the network number and whereas
the last IP address is reserved for broadcast IP.
Class A
IP addresses belonging to class A are assigned to the networks that contain a large number of hosts.
• The network ID is 8 bits long.
• The host ID is 24 bits long.
Class B
IP address belonging to class B is assigned to networks that range from medium-sized to large-sized networks.
• The network ID is 16 bits long.
• The host ID is 16 bits long.
Class C
IP addresses belonging to class C are assigned to small-sized networks.
• The network ID is 24 bits long.
• The host ID is 8 bits long.
Class D
IP address belonging to class D is reserved for multi-casting. The higher-order bits of the first octet of IP
addresses belonging to class D is always set to 1110. The remaining bits are for the address that interested
hosts recognize.
Class E
IP addresses belonging to class E are reserved for experimental and research purposes. IP addresses of class E range
from 240.0.0.0 – 255.255.255.255. This class doesn’t have any subnet mask. The higher-order bits of the first octet of
class E are always set to 1111.
Socket programming
1. Java socket programming is used for communication between the
applications running on different JRE.
2. Java socket programming can be connection-oriented or connectionless.
3. Socket and ServerSocket classes are used for connection-oriented socket
programming and DatagramSocket and DatagramPacket classes are
used for connection-less socket programming.