You are on page 1of 14

Web Technology (KCS 602)

Unit 3
JavaScript: Introduction:

• What is JavaScript? It is a text based programming language used to both on the client-side and
server-side that allows you to make web pages interactive.

JavaScript is a high-level, interpreted programming language. It is one of the three


core technologies of the World Wide Web, along with HTML and CSS. JavaScript is
used to make web pages interactive and dynamic. It can be used to create games,
animations, and other interactive content. JavaScript is also used on the server side
to create web applications. Use libraries for the client-side are AngularJS, ReactJS, VueJS etc.
Famous framework: Node.js.
• History of JavaScript

JavaScript was created by Brendan Eich in 1995. It was originally called Mocha, but
it was renamed JavaScript when it was released by Netscape in 1996. JavaScript
quickly became popular, and it is now the most popular programming language in
the world.

• Features of JavaScript

JavaScript is a versatile language with many features. Some of its key features
include:

Code snippet
* Object-oriented programming: JavaScript is an object-oriented programming
language. This means that code is organized into objects, which are data
structures that contain both data and methods.
* Event-driven programming: JavaScript is an event-driven programming
language. This means that code is executed in response to events, such as
mouse clicks or keyboard input.
* Dynamic typing: JavaScript is a dynamically typed language. This means
that the data type of a variable can change at runtime.
* Cross-platform compatibility: JavaScript is a cross-platform language.
This means that it can be used on any platform that supports a web browser.

• Applications of JavaScript

JavaScript can be used for a wide variety of applications, including:

Code snippet
* Creating interactive web pages: JavaScript can be used to create
interactive web pages that respond to user input.
* Developing games: JavaScript can be used to develop games for the web.
* Creating animations: JavaScript can be used to create animations for the
web.
* Building web applications: JavaScript can be used to build web
applications that run on the server side.

• Learning JavaScript

JavaScript is a relatively easy language to learn. There are many resources


available to help you learn JavaScript, including books, tutorials, and online courses.

• Conclusion

JavaScript is a powerful and versatile language that can be used for a wide variety of
applications. If you are interested in web development, JavaScript is a language that
you should definitely learn.

JavaScript documents:

• JavaScript documents are text files that contain JavaScript code. They are
typically used to add interactivity to web pages.
• JavaScript code is written in a text-based format. It is made up of statements,
which are instructions that the JavaScript interpreter executes.
• Statements can be grouped together into functions. Functions are reusable
blocks of code that can be called from anywhere in a JavaScript document.
• JavaScript documents can be imported into other JavaScript documents. This
allows you to reuse code and avoid duplicating code.
• JavaScript documents can be minified to reduce their size. This can improve
the performance of your web pages.

Here are some of the things you can do with JavaScript documents:

• Add interactivity to web pages. For example, you can use JavaScript to create
animations, games, and other interactive elements.
• Validate forms. You can use JavaScript to check if the data entered into a
form is valid.
• Detect browsers. You can use JavaScript to detect which browser a user is
using.
• Create cookies. You can use JavaScript to create cookies, which are small
files that are stored on the user's computer.
• Make Ajax requests. Ajax is a technique that allows you to make requests to a
server without reloading the entire web page.

If you are interested in learning more about JavaScript, there are many resources
available online. Here are a few suggestions:
• Mozilla Developer Network (MDN): The MDN is a great resource for learning
about JavaScript. It has a comprehensive reference guide, tutorials, and other
resources.
• W3Schools: W3Schools is another great resource for learning about
JavaScript. It has a beginner-friendly tutorial and other resources.
• JavaScript: The Definitive Guide by David Flanagan: This book is a
comprehensive guide to JavaScript. It covers all aspects of the language,
from the basics to advanced topics.

Forms
Forms are used to collect user input. They can be used to collect information such as
names, addresses, email addresses, and phone numbers. Forms can also be used
to submit data to a server.

To create a form in JavaScript, you use the <form> element. The <form> element has
a number of attributes that you can use to control its behavior. For example, you can
use the action attribute to specify the URL that the form data will be submitted to.

Once you have created a form, you can add input elements to it. Input elements are
used to collect different types of user input. For example, you can use the <input
type="text"> element to collect text input, the <input type="password"> element to
collect password input, and the <input type="submit"> element to create a submit
button.

When a user submits a form, the form data is sent to the URL specified by the
action attribute. The server can then process the form data and take appropriate
action.

Statements
Statements are the basic building blocks of JavaScript code. They are instructions
that the JavaScript interpreter executes.

There are many different types of statements in JavaScript. Some common types of
statements include:

• Assignment statements: Assignment statements are used to assign values to


variables.
• Expression statements: Expression statements are used to evaluate
expressions and return a value.
• Control flow statements: Control flow statements are used to control the flow
of execution of a JavaScript program.
• Object literal statements: Object literal statements are used to create object
literals.
• Function declaration statements: Function declaration statements are used to
declare functions.

Functions
Functions are reusable blocks of code. They can be used to encapsulate code and
make it easier to read and maintain.

To create a function in JavaScript, you use the function keyword. The function
keyword is followed by the function name, the function parameters, and the function
body.

The function parameters are the variables that the function will receive when it is
called. The function body is the code that the function will execute.

Once you have created a function, you can call it by using its name. When you call a
function, the JavaScript interpreter will execute the code in the function body.

Functions are a powerful tool that can be used to make JavaScript code more
readable, maintainable, and reusable.

Forms

Code snippet
// Create a form
var form = document.getElementById("myForm");

// Add a text input field


var input = document.createElement("input");
input.type = "text";
input.name = "name";
form.appendChild(input);

// Add a submit button


var button = document.createElement("button");
button.type = "submit";
button.innerHTML = "Submit";
form.appendChild(button);

When the user submits the form, the name input field will be submitted to the server.

Statements

Code snippet
// This is an if statement
if (condition) {
// Do something if the condition is true
} else {
// Do something if the condition is false
}

// This is a for loop


for (var i = 0; i < 10; i++) {
// Do something for each iteration of the loop
}

// This is a while loop


while (condition) {
// Do something while the condition is true
}

Functions

Code snippet
// Define a function
function myFunction(arg1, arg2) {
// Do something with the arguments
}

// Call the function


myFunction(1, 2);

Functions are a powerful way to organize code and make it reusable.

Objects
In JavaScript, an object is a data structure that can store data and methods. Objects
are created using the { } syntax. For example, the following code creates an object
called myObject with two properties, name and age:

Code snippet
const myObject = {
name: "John Doe",
age: 30
};

Objects can be accessed using dot notation. For example, the following code will
print the value of the name property of myObject:

Code snippet
console.log(myObject.name);

Objects can also be used to create custom data types. For example, the following
code creates a custom data type called Person with two properties, name and age:
Code snippet
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}

The Person class can then be used to create new Person objects. For example, the
following code creates a new Person object called john:

Code snippet
const john = new Person("John Doe", 30);

AJAX (Asynchronous JavaScript and XML) is a set of web development


AJAX techniques for creating better, faster and more interactive web
applications with the help of XML, HTML, CSS and JavaScript.
AJAX stands for Asynchronous JavaScript and XML. AJAX is a technique that allows
web pages to update themselves without having to reload the entire page. This can
be used to improve the user experience by making web pages more responsive and
interactive.

AJAX uses the XMLHttpRequest object to send requests to the server. The
XMLHttpRequest object can be used to send requests for data in a variety of formats,
including XML, JSON, and plain text.

To use AJAX, you need to create an XMLHttpRequest object and then use the open()
method to specify the type of request, the URL of the resource you want to request,
and the HTTP method (GET or POST). You can then use the send() method to send
the request to the server.

The server will then send a response back to the client. The XMLHttpRequest object
will have a responseText property that contains the response from the server. You
can then use this property to parse the response and extract the data you need.

Here is an example of how to use AJAX to get the current time from a server:

Code snippet
const xhr = new XMLHttpRequest();
xhr.open("GET", "/api/time");
xhr.send();

xhr.onload = function() {
const time = xhr.responseText;
console.log(time);
};
This code will send a GET request to the /api/time URL on the server. The server
will then respond with the current time. The xhr.onload event handler will be called
when the response is received. The time variable will contain the current time.

AJAX is a powerful technique that can be used to make web pages more responsive
and interactive. It is a valuable tool for any JavaScript developer.

Networking
Networking is the process of connecting computers and other devices together so
that they can communicate with each other. Networks can be small, such as a home
network, or large, such as the Internet.

To communicate with each other, devices on a network must have unique


addresses. These addresses are called IP addresses.

Internet Addressing
Internet addressing is the process of assigning IP addresses to devices on the
Internet. IP addresses are 32-bit or 128-bit numbers that identify a specific device on
the network.

There are two types of IP addresses: IPv4 and IPv6.

• IPv4 is the older version of IP addressing. It uses 32-bit addresses.


• IPv6 is the newer version of IP addressing. It uses 128-bit addresses.

InetAddress
InetAddress is a class in the Java programming language that represents an IP
address. It can be used to get the IP address of a host name, or to get the host
name of an IP address.

Here are some examples of how to use InetAddress:

Code snippet
// Get the IP address of the host name "www.google.com"
InetAddress google = InetAddress.getByName("www.google.com");

// Get the host name of the IP address 172.217.10.142


String googleHost = google.getHostName();

Conclusion
Networking is a complex topic, but it is essential for understanding how the Internet
works. IP addresses are a critical part of networking, and InetAddress is a useful
class in the Java programming language for working with IP addresses.

Factory Methods
A factory method is a special type of method that is used to create objects. Factory
methods are often used when there are multiple ways to create an object, or when
the object creation process is complex.

Factory methods are typically defined as static methods, which means that they can
be called without creating an instance of the class. This makes factory methods
useful for creating objects that are not associated with a specific instance.

Here is an example of a factory method:

Code snippet
class Person {
// Constructor function
constructor(name, age) {
this.name = name;
this.age = age;
}

// Factory method
static create(name, age) {
return new Person(name, age);
}
}

The Person.create() factory method can be used to create a new Person object
without creating an instance of the Person class. For example, the following code
creates a new Person object and assigns it to the variable john:

Code snippet
const john = Person.create("John Doe", 30);

Instance Methods
An instance method is a method that is associated with a specific object. Instance
methods can be used to access and modify the properties of the object.

Instance methods are typically defined as non-static methods, which means that they
can only be called on an instance of the class. This makes instance methods useful
for accessing and modifying the properties of a specific object.

Here is an example of an instance method:

Code snippet
class Person {
// Constructor function
constructor(name, age) {
this.name = name;
this.age = age;
}

// Instance method
getFullName() {
return `${this.name} ${this.age}`;
}

The Person.getFullName() instance method can be used to get the full name of a
Person object. For example, the following code gets the full name of the john object:

Code snippet
const fullName = john.getFullName();

Conclusion
Factory methods and instance methods are both important concepts in JavaScript.
Factory methods are used to create objects, while instance methods are used to
access and modify the properties of objects.

TCP/IP client sockets:

• What is a TCP/IP client socket?

A TCP/IP client socket is a software object that enables two applications to


communicate with each other over a network. It is a bidirectional, stream-based
connection that provides a reliable, ordered, and error-checked delivery of data.
A socket can be used to connect Java I/O system to other programs
• How do TCP/IP client sockets work?

When a client application wants to connect to a server application, it creates a socket


object and specifies the server's IP address and port number. The socket object then
initiates a connection to the server. Once the connection is established, the client
and server can exchange data using the socket object.

• What are the benefits of using TCP/IP client sockets?

There are several benefits to using TCP/IP client sockets, including:

Code snippet
* **Reliable delivery of data:** TCP/IP client sockets provide a reliable,
ordered, and error-checked delivery of data. This means that the data that
is sent by the client will be received by the server in the same order and
without any errors.
* **Bidirectional communication:** TCP/IP client sockets allow for
bidirectional communication between the client and server. This means that
both applications can send and receive data at the same time.
* **Stream-based communication:** TCP/IP client sockets use stream-based
communication. This means that data is sent and received as a continuous
stream of bytes. This is in contrast to packet-based communication, which
sends data in small chunks.

• What are the limitations of using TCP/IP client sockets?

There are a few limitations to using TCP/IP client sockets, including:

Code snippet
* **Performance:** TCP/IP client sockets can be slower than other types of
network connections, such as UDP. This is because TCP/IP sockets use a lot
of overhead to ensure reliable delivery of data.
* **Complexity:** TCP/IP client sockets can be more complex to use than
other types of network connections. This is because they require more
configuration and setup.

• Examples of TCP/IP client sockets

There are many examples of TCP/IP client sockets in use today. Some common
examples include:

Code snippet
* Web browsers: Web browsers use TCP/IP client sockets to connect to web
servers and download web pages.
* Email clients: Email clients use TCP/IP client sockets to connect to mail
servers and send and receive email.
* File transfer programs: File transfer programs use TCP/IP client sockets
to connect to other computers and transfer files.

• Conclusion

TCP/IP client sockets are a powerful tool that can be used to create reliable,
bidirectional, and stream-based connections between applications. They are used in
a wide variety of applications, including web browsers, email clients, and file transfer
programs.

URL and URL Connection:

• What is a URL?

A URL, or Uniform Resource Locator, is a standardized way of specifying the


location of a resource on the internet. A URL typically includes the following
components:

Code snippet
* Protocol: The protocol specifies the type of resource, such as HTTP, FTP,
or HTTPS.
* Server name: The server name is the domain name or IP address of the
computer that hosts the resource.
* Port number: The port number, if specified, identifies a specific service
on the server. For example, the port number for HTTP is 80.
* Path: The path specifies the location of the resource on the server.

• What is a URL Connection?

A URL Connection is an object that represents a connection to a resource that is


identified by a URL. A URL Connection can be used to read data from the resource,
write data to the resource, or both.

• How do URL Connections work?

When a URL Connection is created, it opens a connection to the resource that is


identified by the URL. Once the connection is open, the URL Connection can be
used to read or write data to the resource.

• What are the benefits of using URL Connections?

There are several benefits to using URL Connections, including:

Code snippet
* **Portability:** URL Connections are portable to many different
platforms.
* **Efficiency:** URL Connections are efficient in terms of both memory and
CPU usage.
* **Flexibility:** URL Connections provide a wide range of options for
reading and writing data to resources.

• What are the limitations of using URL Connections?

There are a few limitations to using URL Connections, including:

Code snippet
* **Security:** URL Connections can be susceptible to security attacks,
such as cross-site scripting (XSS).
* **Complexity:** URL Connections can be complex to use, especially for
complex tasks such as uploading files.

• Examples of URL Connections

There are many examples of URL Connections in use today. Some common
examples include:

Code snippet
* Web browsers: Web browsers use URL Connections to download web pages.
* Email clients: Email clients use URL Connections to download email
messages.
* File transfer programs: File transfer programs use URL Connections to
download and upload files.

• Conclusion

URL Connections are a powerful tool that can be used to read and write data to
resources that are identified by URLs. They are used in a wide variety of
applications, including web browsers, email clients, and file transfer programs.

TCP/IP server sockets and datagrams:

• TCP/IP server sockets:

A TCP/IP server socket is a software object that enables one or more applications to
listen for and accept connections from other applications over a network. It is a
bidirectional, stream-based connection that provides a reliable, ordered, and error-
checked delivery of data.

• Datagrams:

A datagram is a small packet of data that is sent over a network without any
guarantee of delivery. Datagrams are typically used for applications where reliability
is not as important as speed, such as streaming media or gaming.

• How TCP/IP server sockets work:

When a server application wants to listen for connections, it creates a socket object
and specifies the port number that it wants to listen on. The socket object then enters
a listening state, waiting for connections from clients. When a client application
connects to the server, the server socket object creates a new socket object to
represent the connection. The server and client can then exchange data using the
socket objects.

• How datagrams work:

When a client application wants to send data to a server, it creates a datagram


object and specifies the destination address and port number. The datagram object
is then sent over the network. When the datagram arrives at the destination server, it
is delivered to the application that is listening on the specified port number.

• Benefits of using TCP/IP server sockets:

There are several benefits to using TCP/IP server sockets, including:

Code snippet
* **Reliable delivery of data:** TCP/IP server sockets provide a reliable,
ordered, and error-checked delivery of data. This means that the data that
is sent by the client will be received by the server in the same order and
without any errors.
* **Bidirectional communication:** TCP/IP server sockets allow for
bidirectional communication between the client and server. This means that
both applications can send and receive data at the same time.
* **Stream-based communication:** TCP/IP server sockets use stream-based
communication. This means that data is sent and received as a continuous
stream of bytes. This is in contrast to packet-based communication, which
sends data in small chunks.

• Benefits of using datagrams:

There are several benefits to using datagrams, including:

Code snippet
* **Speed:** Datagrams are typically faster than TCP/IP server sockets
because they do not require the same level of reliability.
* **Ease of use:** Datagrams are easier to use than TCP/IP server sockets
because they do not require the same level of configuration.

• Limitations of using TCP/IP server sockets:

There are a few limitations to using TCP/IP server sockets, including:

Code snippet
* **Performance:** TCP/IP server sockets can be slower than other types of
network connections, such as UDP. This is because TCP/IP sockets use a lot
of overhead to ensure reliable delivery of data.
* **Complexity:** TCP/IP server sockets can be more complex to use than
other types of network connections. This is because they require more
configuration and setup.

• Limitations of using datagrams:

There are a few limitations to using datagrams, including:

Code snippet
* **Unreliable delivery of data:** Datagrams are not guaranteed to be
delivered. This means that the data that is sent by the client may not be
received by the server.
* **Security:** Datagrams are not as secure as TCP/IP server sockets. This
is because datagrams can be intercepted by other devices on the network.

• Examples of TCP/IP server sockets:

There are many examples of TCP/IP server sockets in use today. Some common
examples include:

Code snippet
* Web servers: Web servers use TCP/IP server sockets to listen for and
accept connections from web browsers.
* Email servers: Email servers use TCP/IP server sockets to listen for and
accept connections from email clients.
* File transfer servers: File transfer servers use TCP/IP server sockets to
listen for and accept connections from file transfer clients.

• Examples of datagrams:

There are many examples of datagrams in use today. Some common examples
include:

Code snippet
* Streaming media: Streaming media applications, such as video chat and
audio streaming, use datagrams to send and receive data.
* Gaming: Online gaming applications, such as multiplayer video games, use
datagrams to send and receive data.

• Conclusion:

TCP/IP server sockets and datagrams are both powerful tools that can be used to
create network connections between applications. They are used in a wide variety of
applications, including web servers, email servers, file transfer servers, streaming
media applications, and gaming applications.

You might also like