How to POST show loading while XMLHttpRequest finishes?

Create a loading element in your HTML page

In order to POST show loading while XMLHttpRequest finishes, you need to create a loading element in your HTML page. This can be done by using the <div> tag and adding a class or id to it. For example, you can create a loading element with the following code:
<div class="loading">
  Loading...
</div>
You can also use the <img> tag to display an image as a loading element. For example, you can use the following code to display an image as a loading element:
<img src="loading.gif" alt="Loading..." class="loading">
You can also use the <span> tag to display a text as a loading element. For example, you can use the following code to display a text as a loading element:
<span class="loading">Loading...</span>
You can also use the <progress> tag to display a progress bar as a loading element. For example, you can use the following code to display a progress bar as a loading element:
<progress class="loading"></progress>
You can also use the <svg> tag to display an SVG animation as a loading element. For example, you can use the following code to display an SVG animation as a loading element:
<svg class="loading">
  <circle cx="50" cy="50" r="20"></circle>
</svg>
You can also use the <canvas> tag to display a canvas animation as a loading element. For example, you can use the following code to display a canvas animation as a loading element:
<canvas class="loading"></canvas>
You can also use the <video> tag to display a video as a loading element. For example, you can use the following code to display a video as a loading element:
<video class="loading">
  <source src="loading.mp4" type="video/mp4">
</video>
You can also use the <audio> tag to display an audio as a loading element. For example, you can use the following code to display an audio as a loading element:
<audio class="loading">
  <source src="loading.mp3" type="audio/mpeg">
</audio>
You can also use the <iframe> tag to display an external page as a loading element. For example, you can use the following code to display an external page as a loading element:
<iframe class="loading" src="https://example.com"></iframe>
Once you have created the loading element, you can use it to show loading while XMLHttpRequest finishes.

Create an XMLHttpRequest object and set the onreadystatechange event handler to a function

In order to POST show loading while XMLHttpRequest finishes, you need to create an XMLHttpRequest object and set the onreadystatechange event handler to a function. This function will be called when the request is complete and will be used to hide the loading element. To create the XMLHttpRequest object, you can use the following code:

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
  // code to hide loading element
};

The onreadystatechange event handler is a function that will be called when the request is complete. Inside this function, you can check the readyState property of the XMLHttpRequest object to determine if the request is complete. If the readyState is 4, then the request is complete and you can hide the loading element. For more information on the XMLHttpRequest object, you can refer to the Mozilla Developer Network.

Inside the onreadystatechange function, check the readyState property of the XMLHttpRequest object. If the readyState is 4 (meaning the request is complete), hide the loading element.

In order to POST show loading while XMLHttpRequest finishes, you need to create a loading element in your HTML page, create an XMLHttpRequest object and set the onreadystatechange event handler to a function. Inside the onreadystatechange function, you need to check the readyState property of the XMLHttpRequest object. If the readyState is 4 (meaning the request is complete), you need to hide the loading element. Before sending the request, you need to show the loading element. To send the request, you need to use the open() and send() methods of the XMLHttpRequest object. When the request is complete, the onreadystatechange event handler will be called and the loading element will be hidden.

// Create a loading element in your HTML page
let loadingElement = document.createElement('div');
loadingElement.className = 'loading';

// Create an XMLHttpRequest object and set the onreadystatechange event handler to a function
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
  // Inside the onreadystatechange function, check the readyState property of the XMLHttpRequest object
  if (xhr.readyState === 4) {
    // If the readyState is 4 (meaning the request is complete), hide the loading element
    loadingElement.style.display = 'none';
  }
};

// Before sending the request, show the loading element
loadingElement.style.display = 'block';

// Send the request using the open() and send() methods of the XMLHttpRequest object
xhr.open('GET', 'url');
xhr.send();

// When the request is complete, the onreadystatechange event handler will be called and the loading element will be hidden

Before sending the request, show the loading element

Before sending an XMLHttpRequest, it is important to show a loading element to the user. This will let them know that the request is being processed and that they should wait for the response. To do this, create a loading element in your HTML page, such as a spinner or a progress bar. Then, create an XMLHttpRequest object and set the onreadystatechange event handler to a function. Inside the onreadystatechange function, check the readyState property of the XMLHttpRequest object. If the readyState is 4 (meaning the request is complete), hide the loading element. Before sending the request, show the loading element. To do this, use the
document.getElementById("loading").style.display = "block";
command. Finally, send the request using the open() and send() methods of the XMLHttpRequest object. When the request is complete, the onreadystatechange event handler will be called and the loading element will be hidden.

Send the request using the open() and send() methods of the XMLHttpRequest object

To send the request using the open() and send() methods of the XMLHttpRequest object, you need to create an XMLHttpRequest object and set the onreadystatechange event handler to a function. Then, inside the onreadystatechange function, you need to check the readyState property of the XMLHttpRequest object. If the readyState is 4 (meaning the request is complete), you can hide the loading element. Before sending the request, you should show the loading element. To send the request, you can use the
open()
and
send()
methods of the XMLHttpRequest object. When the request is complete, the onreadystatechange event handler will be called and the loading element will be hidden. For more information on how to use the open() and send() methods of the XMLHttpRequest object, you can refer to the Mozilla Developer Network.

When the request is complete, the onreadystatechange event handler will be called and the loading element will be hidden.

When the XMLHttpRequest is complete, the onreadystatechange event handler will be called. This event handler will check the readyState property of the XMLHttpRequest object. If the readyState is 4 (meaning the request is complete), the loading element will be hidden. To ensure that the loading element is hidden when the request is complete, you should show the loading element before sending the request. To do this, create a loading element in your HTML page and use the open() and send() methods of the XMLHttpRequest object to send the request. When the request is complete, the onreadystatechange event handler will be called and the loading element will be hidden.

Useful Links