JS Tutorial
JS Version
JS Objects
JS Function
JS Classes
JS Async
JS HTML DOM
JS Browser BOM
JS Web API
JS AJAX
JS JSON
JS vs JQUERY
JS Graphics
AJAX Intro
AJAX offers you to:
- Read data from a web server – after the page is loaded
- Update a web page without reloading the page
- Send data to a web server – in the background
Example
<!DOCTYPE html>
<html>
<body>
<div id=”demo”>
<h2>The XMLHttpRequest Object</h2>
<button type=”button” onclick=”loadDoc()”>Change Content</button>
</div>
<script>
function loadDoc() {
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById(“demo”).innerHTML =
this.responseText;
}
xhttp.open(“GET”, “ajax_info.txt”);
xhttp.send();
}
</script>
</body>
</html>
Output
Example
localStorage.setItem(“name”, “Host Guru”);
The getItem() Method
The localStorage.getItem() method is used to retrieve a data item from the storage.
It takes a name as a parameter.
Example
localStorage.getItem(“name”);
The sessionStorage Object
The sessionStorage object is similar to the localStorage object.
The difference is that the sessionStorage object stores data for one session.
The data is deleted as soon as the browser closes.
Example
<!DOCTYPE html>
<html>
<body>
<div id=”demo”>
<h2>The XMLHttpRequest Object</h2>
<button type=”button” onclick=”loadDoc()”>Change Content</button>
</div>
<script>
function loadDoc() {
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById(“demo”).innerHTML =
this.responseText;
}
xhttp.open(“GET”, “ajax_info.txt”);
xhttp.send();
}
</script>
</body>
</html>
The HTML page consists of a <div> section and a <button>.
The <div> section displays information from a server.
The <button> calls a function (if it is clicked).
The function requests data from a web server and displays it:
Example
Function loadDoc()
function loadDoc() {
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById(“demo”).innerHTML = this.responseText;
}
xhttp.open(“GET”, “ajax_info.txt”, true);
xhttp.send();
}
What is AJAX?
AJAX = Asynchronous JavaScript And XML.
AJAX is not a programming language.
AJAX just uses a combination of:
- A browser built-in XMLHttpRequest object (to request data from a web server)
- JavaScript and HTML DOM (to display or use the data)
AJAX enables web pages to be updated asynchronously with the help of exchanging data with a web server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.
How AJAX Works

An event occurs on a web page (the page is loaded; a button is clicked)
- An XMLHttpRequest object is created by JavaScript
- The XMLHttpRequest object sends a request to a web server
- The server processes the request.
- The server sends a response back to the web page
- The response is read by JavaScript
- Proper action (like page update) is performed by JavaScript
Modern Browsers (Fetch API)
Modern Browsers use Fetch API instead of the XMLHttpRequest Object.
The Fetch API interface enables a web browser to make HTTP requests to web servers.
If you use the XMLHttpRequest Object, Fetch can do the same in a simpler way.