Module 04 Modern Web Applications
Module 04 Modern Web Applications
<body>
<p id=“csbs”>CSBS Dept of ISE</p>
<script type=“text/javascript”>
$(document).ready(function( ){
$(“#csbs").click(function(){
$(this).hide();
});
});
</script>
</body>
</html>
<html>
<head>
<title>Example of JavaScript event handler</title>
</head>
<body>
<p id=“csbs”>CSBS Dept of ISE</p>
<script type=“text/javascript”>
document.getElementById(“csbs”).onclick = function () {
document.getElementById(“csbs”).style.display = “none”;
};
</script>
</body>
</html>
JQuery Effects
• show() element will be visible
• hide() element will be hidden
• toggle() element will toggle between hide or show
• fadeIn() slowly the element will appear on screen
• fadeOut() slowly the element will disappear
• fadeToggle() toggle between appear and disappear
• fadeTo(“ms”, “%”) opacity will be added based on input
• slideUp() animation of sliding up to hide
• slideDown() animation of sliding down to visible
• slideToggle() toggle between slideUp and slideDown
JQuery DOM Manipulation
• text() sets or return the exiting value of element
• html() sets or return the existing value with markup
• val() sets or return the value of form elements
• attr() adds or get the value of attribute of element
• append() add content at the end of selected element
• prepend() add content at the starting of selected element
• after() add content after the of selected element
• before() add content before the of selected element
• remove() remove all the children and the element
• empty() removes only children but not the element
CSS manipulation
• addClass()
– Adds one or more classes to the selected elements
– $(“p”).addClass(“bg-dark”);
• removeClass()
– Removes one or more classes from the selected elements
– $(“p”).removeClass(“bg-dark”);
• toggleClass()
– Toggles between adding/removing classes from the selected elements
– $(“p”).toggleClass(“bg-dark”);
• css()
– Sets or returns the style attribute
– $(“p”).css(“background-color”, “blue”);
– $("p").css({"background-color": "yellow", "font-size": "200%"});
Height and width respect to Box model
• width()
• height()
• innerWidth()
• innerHeight()
• outerWidth()
• outerHeight()
• outerWidth(true)
• outerHeight(true)
Traversing
• Finding an other element by using the relation
of other element with current element in the
DOM Tree.
• If the current element is UL
• Parent id DIV
• Child is LI
• parent()
Traversing Ancestors
– Used to find the parent of current element
– Syntax: $(“ul").parent();
– Example:
• $(“ul").parent().addClass(“pb-5”);
• parents()
– Used to find all the parents of current element until html
– Syntax: $(“ul").parents();
– Example:
• $(“ul").parents().addClass(“pb-5”);
• parentsUntil()
– Used to find all the parents of current element until specified parent
– Syntax: $(“ul").parentsUntil(“parent name”);
– Example:
• $(“div").parentsUntil(“div”).css({"border": "2px solid red"});
Traversing Descendants
• children()
– Used to select the children of the current element
– Syntax: $(“ul").children();
– Example:
• $(“ul").children().addClass(“mb-5”);
• $("div").children("p.first") .addClass(“mb-5”);
• find()
– Used to find selected element on any level of the DOM Tree
– If * is passed to find the child then all children will be selected
– Syntax: $(“ul").find(“span”);
– Example:
• $(“ul").find(“span”).addClass(“mb-5”);
• $(“ul").find(“*”). .addClass(“mt-5”);
Traversing Siblings
• siblings()
– Used to select all sibling elements of the selected element
– Also can select the siblings of particular element by selectors
– Syntax: $(“selector").siblings();
– Example:
• $(“div").siblings().hide();
• $(“div").siblings(“div”).hide();
• next()
– Used to select the next sibling of current element.
– Syntax: $(“div").next();
– Example: $(“div").next().hide();
• nextAll()
– Used to select the next sibling of current element.
– Syntax: $(“div").nextAll();
– Example: $(“div").nextAll().hide();
Traversing Siblings
• nextUntil()
– Used to select the next siblings of current element until the specified sibling.
– Syntax: $(“div").nextUntil(“h1”);
– Example: $(“div").nextUntil(“h1”).hide();
• prev()
– Used to select the previous sibling of current element.
– Syntax: $(“div").prev();
– Example: $(“div").prev().hide();
• prevAll()
– Used to select the previous siblings of current element.
– Syntax: $(“div").prevAll();
– Example: $(“div").prevAll().hide();
• prevUntil()
– Used to select the previous siblings of current element until the specified sibling.
– Syntax: $(“div").prevUntil(“h1”);
– Example: $(“div").prevUntil(“h1”).hide();
Traversing filtering
• first()
– Used to select the first element of the specified elements
– Syntax: $(“div").first(“h1”);
– Example: $(“div").first(“h1”).css(“color”,“red”);
• last()
– Used to select the last element of the specified elements
– Syntax: $(“div").last(“h1”);
– Example: $(“div").last(“h1”).hide();
• eq()
– Used to select the element based on the index and index starts with 0
– Syntax: $(“div").eq(1);
– Example: $(“div").eq(0).hide();
• filter()
– Used to filter the elements and selects list of elements
• not()
– Works opposite to the filter()
AJAX
• AJAX is the art of exchanging data with a server, and updating
parts of a web page - without reloading the whole page.
• AJAX = Asynchronous JavaScript and XML
• Examples of applications using AJAX: Gmail, Google Maps,
Youtube, and Facebook tabs.
• With the jQuery AJAX methods, you can request text, HTML,
XML, or JSON from a remote server
• AJAX applications are much more faster and responsive as
compared to traditional web applications.
• On 18 February 2005 for the first time, Jesse James Garrett
introduce AJAX
• on 5th April 2006, the W3C(world wide web consortium) release
the first draft which contains the specifications for the
XMLHttpRequest object. After that AJAX will be popular
among web developers.
Steps how AJAX works
• A client event occurs.
• An XMLHttpRequest object is created.
• The XMLHttpRequest object is configured.
• The XMLHttpRequest object makes an asynchronous
request to the Webserver.
• The Webserver returns the result containing XML
document.
• The XMLHttpRequest object calls the callback()
function and processes the result.
• The HTML DOM is updated.
Advantages of AJAX
• It creates responsive and interactive web
applications.
• It supports the development of patterns and
frameworks that decrease the development time.
• It makes the best use of existing technology and
feature instead of using some new technology.
• It makes an asynchronous call to the web server
which means the client doesn't have to wait for
the data to arrive before starting rendering
AJAX
• AJAX applications initiate XMLHttpRequest to manage data requests
sent to the web servers and handle or monitor the data sent by the web
servers in a very effective manner. AJAX support the following types of
requests
– GET
• To retrieve data from a server
• The data is sent as a part of the URL that is appended at the end of the request
– POST
• To send data from a web page to a web server.
• The data is sent in the request body that is separated from the URL
– DELETE
• To delete data from the web server
• The data to be deleted is sent in the request body and the web server will delete that
data from its storage
– PUT
• To send data from a web page to a web server to update the existing content in the
server.
• The data is sent in the request body that is separated from the URL
AJAX
• To create a connection and send a request to the
web server AJAX supports the methods
– open() to create the connection
– send() to send the created connection
– open()
• Syntax: open(request method , url , async, user, password)
• Example:
Var csbsrequest = new XMLHttpRequest();
csbsrequest.open(“GET”,“https://jssstu.in/csbs.html”,true)
– send()
• csbsrequest.send()
AJAX Methods
• $.ajax(url, type, data, success, error);
• $.get(url, response);
• $.post(url, data, response);
• $.getJSON(url, response);
• $.getScript(url);
• $.load(url);
AJAX Method
• load(“url parameter”);
• load(“url parameter”, function(responseTxt, statusTxt, xhr))
When the server acts like a gateway and does not receive
504 Gateway Timeout
a response from the other server(upstream) on time.
HTTP Version Not When the server does not support the version of the
505
Supported HTTP protocol.
Network Authentication When the client needs to authenticate to gain access to
511
Required the network.
Disadvantages of AJAX
• AJAX is fully dependent on Javascript. So if
JavaScript is disabled in your web browser
then you are not able to run the AJAX
webpage
• The debugging of AJAX applications is
difficult.
• If one request can fail then it may fail the load
of the complete webpage.
Get and Post method vs AJAX
• Get
– requests data from the server with an HTTP GET
request
– Syntax
• $.get(URL,callback);
– Example
• $.get("data.txt", function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
Get and Post method vs AJAX
• Post
– requests data from the server with submission of
data to be processed with an HTTP POST request
– Syntax
• $.post(URL,{data_to : “process”},callback);
– Example
• $.post("data.txt",{ name: “A”}, function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
Get vs Post
• GET requests can be cached
• GET requests remain in the browser history
• GET requests can be bookmarked
• GET requests should never be used when dealing with sensitive data
• GET requests have length restrictions
• GET requests are only used to request data (not modify)
• <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/
dist/css/bootstrap.min.css" rel="stylesheet">
• <script src="https://cdn.jsdelivr.net/npm/
bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
Syntax of bootstrap
• Open tag of element
• class as an attribute
• The style needs to be applied should be the
value
• Ex:
<p class=“border”> CSBS </p>
Example of Bootstrap style
<div class="container">
<h1>CSBS First Bootstrap Page</h1>
<p>Information Science and Engineering .</p>
</div>
Extra Small Medium Large Extra XXL
small ≥576px ≥768px ≥992px Large ≥1400px
<576px ≥1200px
max- 100% 540px 720px 960px 1140px 1320px
width
Example of Bootstrap style
• <div class="container pt-5"></div>
– Gives padding top 5
• <div class="border"></div>
– Adds default border to element
• <div class=“bg-dark"></div>
– Adds dark background color
Variations in container
Class Extra small Small Medium Large Extra large XXL
<576px ≥576px ≥768px ≥992px ≥1200px ≥1400px
const root =
ReactDOM.createRoot(document.getElementById('root'));
root.render(<p>Hello World</p>);
ReactJS with CDN
• ReactJS also has two versions of CDN
– Development
• <script crossorigin
src="https://unpkg.com/react@18/umd/react.development.js"></scri
pt>
• <script crossorigin
src="https://unpkg.com/react-dom@18/umd/react-
dom.development.js"></script>
– Production
• <script crossorigin
src="https://unpkg.com/react@18/umd/react.production.min.js"></s
cript>
• <script crossorigin
src="https://unpkg.com/react-dom@18/umd/react-
dom.production.min.js"></script>
Example
<html>
<body>
<div id="react-app"></div>
<script src="https://unpkg.com/react@17/umd/react.development.js"
crossorigin></script>
<script src="https://unpkg.com/react-dom@17/umd/react-
dom.development.js" crossorigin></script>
<script language="JavaScript">
element = React.createElement('h1', {}, 'Hello React!')
ReactDOM.render(element, document.getElementById('react-app'));
</script>
</body>
</html>
Advantages of ReactJS
• Performance
• Easy to learn
• Huge collection of third party components
• Large community
• SEO Friendliness
• Easy kick-starting of the React project
• Rich set of developer tools
• Handle large application
Class
- React development is based on components
- Components are self-contained, reusable units
of UI and logic
- React projects typically contain many
components
- While React components can be a function or
a class
Class
• React Class Components are JavaScript
classes that extend React.Component
• They define the UI, and handle events within
the application
• The render() method in react class components
returns JSX elements describing the UI of the
Application.
Example of default class
// Filename App.js
return (
<form>
<label>Enter your name:
<input type="text" value={name}
onChange={(e) => setName(e.target.value)} />
</label>
</form>
)
}
function CSBS() {
const myaction = () => { alert(“Button Clicked!"); }
return (<button onClick={myaction}>Click here!</button>);
}
const root =
ReactDOM.createRoot(document.getElementById('root'));
root.render(<CSBS />);
Event handlers
• onClick
• onDrag
• onDoubleClick
• onKeyDown
• onKeyPress
• onKeyUp
• onChange
• onFocus
• onBlur
• onSubmit
Conditionals
) ;
1>
function Goal(props) { { </h
l ( ) ss
const isGoal = props.isGoal; oa c ce > );
eG >Su ) { /h1
if (isGoal) { a d 1 a l( re<
M (< h o ilu
return <MadeGoal/>; i on n d G a
c t u r sse > F
un r e t i 1
} f M (< h
n
io urn
return <MissedGoal/>; c t t
fu n re
}
}
}
const root =
ReactDOM.createRoot(document.getElementById('root'));
root.render(<Goal isGoal={false} />)
Lists
import React from 'react';
in ReactJS
import ReactDOM from 'react-dom/client';
function Garage( ) {
const cars = ['Ford', 'BMW', 'Audi'];
return ( <div>
<h1>Cars in my garage?</h1>
<ul>
{cars.map((car) => <CarName brand={car} />)}
</ul>
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));